Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence. For example 10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30.
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”.
Precedence and Associativity are two characteristics of operators that determine the evaluation order of subexpressions in absence of brackets.
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 following program is in-fact compiler dependent. See this for details.
2) All operators with same precedence have same associativity
This is necessary, otherwise there won’t be any way for compiler to decide evaluation order of expressions which have two operators of same precedence and different associativity. For example + and – have 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.
5) There is no chaining of comparison operators in C
In Python, expression like “c > b > a” is treated as “a > b and b > c”, 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)
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 readability of the code as the reader doesn’t have to see the table to find out the order.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Output of C programs | Set 64 (Pointers)
- Bitwise Operators in C/C++
- C | Advanced Pointer | Question 10
- Difference between ++*p, *p++ and *++p
- C | Operators | Question 12
- C | Operators | Question 10
- Sequence Points in C | Set 1
- A comma operator question
- Precedence of postfix ++ and prefix ++ in C/C++
- Modulus on Negative Numbers
- C/C++ Ternary Operator - Some Interesting Observations
- Comma in C and C++
- Evaluation order of operands
- Operands for sizeof operator
- Write a C macro PRINT(x) which prints x