Complete C89 operator precedence and associativity table
Complete C89 operator precedence and associativity table
C precedence groups operators; associativity sets grouping at equal levels. The table lists 15 C89 levels, but precedence does not define evaluation order.
| Category | Cheat sheets |
|---|---|
| Kind | Cheat sheets |
| C standard | C89 / C90 |
| Precedence | Category | Operators | Associativity | Example |
|---|---|---|---|---|
| 1 (highest) | Postfix | [] () . -> ++ -- | Left-to-right | a[i]++, f(x), p->member |
| 2 | Unary | ++ -- + - ! ~ * & sizeof (type) | Right-to-left | *p++, !flag, (int)x |
| 3 | Multiplicative | * / % | Left-to-right | a * b / c |
| 4 | Additive | + - | Left-to-right | a + b - c |
| 5 | Shift | << >> | Left-to-right | a << 2 + 1 means a << (2 + 1) |
| 6 | Relational | < <= > >= | Left-to-right | a < b == c means (a < b) == c |
| 7 | Equality | == != | Left-to-right | a == b != c |
| 8 | Bitwise AND | & | Left-to-right | flags & MASK |
| 9 | Bitwise XOR | ^ | Left-to-right | flags ^ MASK |
| 10 | Bitwise OR | | | Left-to-right | flags | MASK |
| 11 | Logical AND | && | Left-to-right; the right operand is not evaluated if the left is false | a != 0 && b / a > 1 |
| 12 | Logical OR | || | Left-to-right; the right operand is not evaluated if the left is true | p == 0 || *p == 0 |
| 13 | Conditional | ?: | Right-to-left; only the selected expression branch is evaluated | a ? b : c ? d : e |
| 14 | Assignment | = += -= *= /= %= <<= >>= &= ^= |= | Right-to-left | a = b = 0 |
| 15 (lowest) | Comma | , | Left-to-right; the result is the value of the rightmost operand | (a = 1, a + 4) |
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
a + b * c = 14
(a + b) * c = 20
1 << 2 + 1 = 8
(1 << 2) + 1 = 5
0 ? a : b + c = 7
comma assignment = 5
x = 1
ℹ️ Info Precedence is not a complete evaluation-order rule. Except for sequencing rules such as those for &&, ||, ?:, and the comma operator, do not rely on the order of subexpression evaluation or modify the same object more than once between sequence points.
💡 Tip In the precedence table, postfix parentheses denote a function call, while parentheses surrounding an expression provide explicit grouping; (type) is the unary cast operator.
🏷️ Related cheat sheets
📚 Related Tutorials
❓ FAQ
QWhy is *p++ equivalent to *(p++) rather than (*p)++?
APostfix ++ has higher precedence than unary *, so p++ is grouped first and its old pointer value is dereferenced. The pointer advances; the pointed-to object is not incremented by this ++. Use (*p)++ to increment the pointed-to value.
QHow does a = b = 0 group, and does that determine the order of object updates?
AAssignment associates right-to-left, producing a = (b = 0); the right-hand assignment expression supplies the value needed by the outer assignment. This grouping does not imply an order between the updates to the objects a and b.
QWhy can't precedence determine the result of a++ + ++a?
APrecedence determines grouping, not the order in which the two subexpressions are evaluated. This expression modifies a more than once between sequence points, so it has undefined behavior in C89. Parentheses and precedence tables cannot make the result portable; use separate statements instead.