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.

CategoryCheat sheets
KindCheat sheets
C standardC89 / 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏