An expression is a combination of operators and operands
Unary operators require only 1 operand, e.g. line 6 below.
Binary operators require 2 operands, e.g. line 9 below.
There is only one ternary operator in the C language, which is the conditional operator ? : that requires 3 operands, e.g. line 12 below.
Automatic data type conversion
The C compiler automatically converts the value on the right side of an assignment operation to match the data type of the variable on the left side.
Arithmetic operators
Operator
Example
Meaning
*
x * y
Multiply x and y
/
x / y
Divide x by y
%
x % y
The remainder after dividing x by y
+
x + y
Add x and y
-
x - y
Subtract y from x
+
+x
Value of x
-
-x
Arithmetic negative of x
++
++x
Increase x by 1 before using x
--
--x
Decrease x by 1 before using x
++
x++
Increase x by 1 after using x
--
x--
Decrease x by 1 after using x
Use arithmetic operators to perform the tasks below:
Count 1 to 10
Count 10 to 1
Count odd numbers 1 to 10
Count multiples of 5 from 100 to 50
Evaluation trees show order of precedence
Operators in C arithmetic expressions follow the same order of precedence as normal math expressions.
Parentheses are the highest precedence
Unary operators, such as ++ or --
Positive and negative sign operators, e.g. + or -
Multiplication and division operators
Addition and subtraction operators
Assignment operators are the lowest precedence, such as = or *=
Parentheses can make programs easier to read
Like math expressions, parentheses can be used to indicate specific orders of precedence. The use of extra parentheses in a complex arithmetic expression can make it easier to read and change later.
Division depends on the data type of the operands
The result of division using all integer operands is an integer value. If a fractional result is required, then one of the operands must be forced to be float or double type.
Assignment operators
Operator
Example
Meaning
=
x = y
Assign the value of y to the variable x
*=
x *= y
Assign the product of x multiplied by y to the variable x
/=
x /= y
Assign the quotient of x divided by y to the variable x
+=
x += y
Assign the sum of x and y to the variable x
-=
x -= y
Assign the result of subtracting y from x to the variable x
When using the combination assignment operators, e.g. *=, the expression on the right side of the operator is fully evaluated before the assignment operator is applied.
Comparison operators only have 2 possible values
Value 1 to mean logical true
Value 0 to mean logical false
Operator
Example
Meaning
<
x < y
1 if x is less than y
<=
x <= y
1 if x is less than or equal to y
>
x > y
1 if x is greater than y
>=
x >= y
1 if x is greater than or equal to y
==
x == y
1 if x is equal to y
!=
x != y
1 if x is not equal to y
Be careful using equal signs
Errors often happen with the use of the equal signs:
One equal sign, = , is the assignment operator
Two consecutive equal signs, == , is the equality comparison operator
Logic operators only have 2 possible values
Non-zero value to mean logical true
0 value to mean logical false
Operator
Example
Meaning
&&
x && y
1 if the values of x and y are both not 0
||
x || y
1 if the values of x or y are not 0
!
!x
1 if the value of x is equal to zero.
Review questions
Working with numbers
In the C program below, what should replace the XXX placeholder at line 7?
#include <stdio.h>
int main(void)
{
int age;
scanf("%d", XXX );
age += 3;
printf("You will be %d years old after 3 years of university.", age );
}
Review questions
Working with numbers
In the C program below, how could you replace the unary operator with a binary operator?
#include <stdio.h>
int main(void)
{
int age;
scanf("%d", &age );
age += 3;
printf("You will be %d years old after 3 years of university.", age );
}
Review questions
Working with numbers
In the C program below, what is the displayed output?