Precedence and Associativity of Operators in C
Precedence is the priority assigned to operators. When more than one operator appears in an expression, they will be evaluated as per their precedence.
In expression 10+6/2, division operator (/) has more priority than addition operator (+), so 6/2 will be evaluated followed by adding 10 the the result of 6/2 i.e. 3.
Associativity is the way to specify the order when operators of same precedence appear in expression.
Table of Precedence and Associativity in C Language
Level | OPERATORS | ASSOCIATIVITY |
1 | (), [] | Left to Right |
2 | !, ~, ++, –, Unary +, Unary -, *, &, (type), sizeof() ) | Right to Left |
3 | *, /, % | Left to Right |
4 | + , – | Left to Right |
5 | << , >> | Left to Right |
6 | <, <= , > , >= | Left to Right |
7 | = =, != | Left to Right |
8 | & | Left to Right |
9 | ^ | Left to Right |
10 | | | Left to Right |
11 | && | Left to Right |
12 | || | Left to Right |
13 | ?: | Right to Left |
14 | =, +=, -=, /=, *=, ^=, !=, ,=, >>= | Left to Right |
15 | COMMA OPERATOR(,) | Left to Right |
Popular Books of Computer Science