3.operators in C
3.operators in C
Chandula Rajapaksa
1
Operators in C
Objective:
Division / H1 / 5 10 / 3 = 3
3
Operator Precedence and
Associativity
▪ Operator precedence establishes the priority of
an operator in relationship to all other operators.
▪ Parentheses can be used to modify the normal
order of execution of an expression.
▪ Operator associativity establishes the order in
which operators of the same precedence are to
be executed.
4
Operator Precedence and
Associativity
Order Operator Associativity
1 () Parenthesis Left to Right
2 * Multiplication Left to Right
/ Division
% Remainder
5
Operator Precedence and
Associativity
▪ Example : 55 / 10 % 2 * 5 -10 % (4-2)
= 55 / 10 % 2 * 5 – 10 % 2
= 5 % 2 * 5 – 10 % 2
= 1 * 5 – 10 % 2
= 5 – 10 % 2
=5–0
=5 6
Operator Precedence and
Associativity
▪ Exercises :
1. 2 * 3 / 4 * 5 – 2
2. 2 * ( 3 + 4 ) % 3 + 3
3. -75 / 25 + 10 * 3
4. 50 % 3 + 2 * 2
5. 2 * 2 * 6 – 7 / 2
7
Equality & Relational Operators
▪ Equality and relational operators test the relationship between two expressions
and yields true or false
Operation Operator C Expression
Equal to == x == y
Not Equal to != x != 2
Greater than > x>6
Less than < x<y
Greater than or equal to >= x >= 9
10
Assignment Operators
▪ There are several assignment operators for abbreviating
assignment expressions.
variable = variable operator expression;
▪ can be written as
variable operator= expression;
where operator is one of the binary operators +, -, *, / or %
Example : a = a+2 is as same as a += 2
11
Increment & Decrement
Operators
▪ ++ increment operator
▪ -- decrement operator
13
14