Operator Precedence and Associativity in C
Operator precedence determines which operation is performed first in an expression with more than one operators with different precedence.
For example: Solve
10 + 20 * 30
10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30
Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left.
For example: ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.
Operators Precedence and Associativity are two characteristics of operators that determine the evaluation order of sub-expressions in absence of brackets
For example: Solve
100 + 200 / 10 - 3 * 10
1) Associativity is only used when there are two or more operators of same precedence.
The point to note is associativity doesn’t define the order in which operands of a single operator are evaluated. For example, consider the following program, associativity of the + operator is left to right, but it doesn’t mean f1() is always called before f2(). The output of the following program is in-fact compiler dependent. See this for details.
2) All operators with the same precedence have same associativity
This is necessary, otherwise, there won’t be any way for the compiler to decide evaluation order of expressions which have two operators of same precedence and different associativity. For example + and – have the same associativity.
3) Precedence and associativity of postfix ++ and prefix ++ are different
Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left. See this for examples.
4) Comma has the least precedence among all operators and should be used carefully For example consider the following program, the output is 1. See this and this for more details.
5) There is no chaining of comparison operators in C
In Python, expression like “c > b > a” is treated as “c > b and b > a”, but this type of chaining doesn’t happen in C. For example consider the following program. The output of following program is “FALSE”.
Please see the following precedence and associativity table for reference.
|( ) |
++ – –
|Parentheses (function call) (see Note 1) |
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement (see Note 2)
|++ – – |
|Prefix increment/decrement |
Logical negation/bitwise complement
Cast (convert value to temporary value of type)
Address (of operand)
Determine size in bytes on this implementation
|* / %||Multiplication/division/modulus||left-to-right|
|<< >>||Bitwise shift left, Bitwise shift right||left-to-right|
|< <= |
|Relational less than/less than or equal to |
Relational greater than/greater than or equal to
|== !=||Relational is equal to/is not equal to||left-to-right|
|^||Bitwise exclusive OR||left-to-right|
||||Bitwise inclusive OR||left-to-right|
|| |||Logical OR||left-to-right|
|? :||Ternary conditional||right-to-left|
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
|,||Comma (separate expressions)||left-to-right|
It is good to know precedence and associativity rules, but the best thing is to use brackets, especially for less commonly used operators (operators other than +, -, *.. etc). Brackets increase the readability of the code as the reader doesn’t have to see the table to find out the order.