C Reference ยท Operators and Expressions
Operator precedence and associativity in C
Reference table of C operator precedence and associativity from postfix to comma, with the classic traps: pointer ++, bitwise vs comparison, and shifts.
Precedence decides which operators bind first when an expression has several; associativity decides the grouping when neighbouring operators have the same precedence. Both are fixed by the grammar of the language and are separate from the order in which operands are evaluated, which is mostly unspecified (see sequence points).
When in doubt, add parentheses. A few well-placed ones cost nothing and prevent the classic mistakes covered below.
int r = a + b * c; /* b * c first */ int t = a - b - c; /* (a - b) - c: left to right */ a = b = c; /* a = (b = c): right to left */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
From highest to lowest precedence:
| Level | Operators | Associativity |
|---|---|---|
| 1 | () call, [], ., ->, postfix ++ --, compound literal (type){...} |
left to right |
| 2 | prefix ++ --, unary + -, ! ~, cast (type), * dereference, & address, sizeof, alignof |
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 | = += -= *= /= %= <<= >>= &= ^= |= |
right to left |
| 15 | , |
left to right |
Errors / undefined behavior: precedence never causes undefined behavior by itself, but expressions such as i = i++ + 1 are undefined because of unsequenced modifications, however they are grouped.
Usage notes
- Bitwise operators sit below comparisons.
x & 1 == 0isx & (1 == 0), which is always 0. Write(x & 1) == 0. This is the single most common precedence bug in C. - Shifts are below
+and-.1 << n + 1is1 << (n + 1). - Postfix beats prefix.
*p++is*(p++);*++pincrements first;(*p)++increments the target.&a[i]is&(a[i]). - Mixing
&&and||without parentheses is legal (&&is higher), but compilers warn with-Wparenthesesbecause readers often get it wrong. - Assignment is right associative and has an expression value, so
a = b = 0works, andif (x = 5)is an assignment, not a comparison (another reason for the warning). - The conditional operator is lower than
||and higher than assignment:x = a || b ? 1 : 2isx = ((a || b) ? 1 : 2). - Unary minus and
sizeof:-x * yis(-x) * y;sizeof x + 1is(sizeof x) + 1. - Precedence is not evaluation order. In
f() + g() * h()the multiplication groups first, but the three calls can happen in any order. - Member access and calls are at the top, so
p->items[i].namereads left to right without parentheses.