C Lecture 5 Part 2
C Lecture 5 Part 2
1
Operator Precedence and Associativity in C
2
Operator Precedence and Associativity in C
Precedence Operator
Description Associativity
* Dereference Operator
& Addressof Operator
sizeof Determine size in bytes
3
Operator Precedence and Associativity in C
4
Operator Precedence and Associativity in C
11 && Logical AND Left-to-Right
12 || Logical OR Left-to-Right
= Assignment
14 Right-to-Left
%= , &= Modulus, bitwise AND assignment
5
Operator Precedence:
Example: 10 + 20 * 30
Note: As we can see, the expression is evaluated as,10 + (20 * 30) but not as
(10 + 20) * 30 due to * operator having higher precedence.
6
Operator Associativity
` Example: 100 / 5 % 2
7
Operator Precedence and Associativity
8
Notes to Remember:
1. Associativity is only used when there are two or more operators of the
same precedence.
3. All operators with the same precedence have the same associativity.
5. Comma has the least precedence among all operators and should be used
carefully.