C Operator Precedence
C operator precedence defines how expressions are grouped when parentheses are not present. Precedence determines which operators bind tighter, and associativity determines how operators of the same precedence level are grouped. When in doubt, parenthesize explicitly. The compiler will not guess your intent.
The list below is ordered from highest precedence to lowest.
Highest precedence operators are evaluated first.
If an expression mixes arithmetic, bitwise, and logical operators, parenthesize.
If you have to remember this table while reading code, the code needs parentheses.
Precedence is not evaluation order. Side effects depend on sequencing rules, not just precedence.
1. Postfix and access (left to right)
Postfix and access operators. These bind the tightest and almost always happen before anything else.
++ -- postfix increment and decrement
() function call
[] array subscript
. structure or union member
-> structure or union member via pointer
(type){list} compound literal (C99)
These operators attach directly to an object or expression and are evaluated immediately. For example, a[i++] increments after indexing, not before.
2. Unary (right to left)
Unary operators. These apply to a single operand and associate from the right.
++ -- prefix increment and decrement
+ - unary plus and minus
! ~ logical NOT and bitwise NOT
(type) cast
* indirection (dereference)
& address-of
sizeof size of object or type
_Alignof alignment requirement (C11)
Casts live here, which is why (int)*p means cast the dereferenced value, not dereference an int. Unary * and & do not behave like multiplication or bitwise AND here.
3. Multiplicative (left to right)
* / % multiplication, division, remainder
4. Additive (left to right)
+ - addition and subtraction
Normal math rules apply.
5. Shifts (left to right)
<< >> bitwise left and right shift
Lower precedence than addition. a + b << c is (a + b) << c.
6. Relational (left to right)
< <=
> >=
Result is an integer, not a boolean type.
7. Equality (left to right)
== !=
8. Bitwise AND (left to right)
& bitwise AND
9. Bitwise XOR (left to right)
^ bitwise XOR
10. Bitwise OR (left to right)
| bitwise OR
Bitwise operators have lower precedence than comparisons. This is a common source of bugs, especially with flags.
11. Logical AND (left to right)
&& logical AND
12. Logical OR (left to right)
|| logical OR
13. Conditional (right to left)
?: conditional (ternary) operator
Associates right to left. Readability drops fast without parentheses.
14. Assignment (right to left)
Assignment operators.
= simple assignment
+= -= add and subtract assignment
*= /= %= multiply, divide, remainder assignment
<<= >>= shift assignment
&= ^= |= bitwise assignment
15. Comma (left to right)
, comma operator
Evaluates left, discards it, then evaluates right. Not the same as commas in argument lists.
https://ftrv.se/3
https://wiki.xxiivv.com/site/ansi_c.html
https://www.kernel.org/doc/man-pages/