Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

C++ operator precedence and associativity

Reference table of C++ operator precedence and associativity, with the classic traps: shifts versus addition, bitwise versus comparison, and evaluation order.

Precedence decides which operator in an expression gets which operands; associativity decides how operators of the same level group. Together they define how the compiler parses a + b * c == d && e. Parentheses override both.

Neither rule says anything about the order in which operands are evaluated. In f() + g() * h() the multiplication groups first, but the three calls may happen in any order. C++17 fixed the order for a few operators (listed below), not for arithmetic.

C++
int r = 2 + 3 * 4 << 1;   // (2 + (3 * 4)) << 1 == 28

Try it

C++ Precedence in action

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 std::cin or std::getline), it gets the next line from this box.

C++ The bitwise-versus-comparison trap

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 std::cin or std::getline), it gets the next line from this box.

Syntax

Groups are listed from the highest precedence (binds tightest) to the lowest.

Level Operators Associativity
1 :: left to right
2 a++ a-- f() a[i] . -> T(x) T{x} typeid const_cast etc. left to right
3 ++a --a +a -a ! ~ (T)a *p &x sizeof co_await new delete right to left
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 | left to right
14 && left to right
15 || left to right
16 ?: = += -= *= /= %= <<= >>= &= ^= |= throw co_yield right to left
17 , left to right

Evaluation order guarantees (C++17 and later): a << b and a >> b evaluate a first; a.b, a->b, a[b], a.*b, a->*b and a(b) evaluate a first; for assignments a = b and a += b, the right operand is evaluated first; &&, || and , evaluate left then right, and &&/|| short-circuit. For ordinary arithmetic operands and function arguments, the order stays unspecified.

Usage notes

  • Add parentheses when a bitwise operator mixes with a comparison, a shift mixes with +, or && mixes with ||. Readers (and compilers, via -Wparentheses) will thank you.
  • Unary beats binary: -a * b is (-a) * b. *p++ is *(p++), since postfix ++ is tighter than unary *.
  • Precedence is not evaluation order: i = i++ + ++i and f(i++, i++) are bad code. Since C++11 an unsequenced modification and use of the same scalar is undefined behavior; see undefined behavior.
  • Stream output: << has lower precedence than + but higher than ==, &, && and ?:. So cout << a == b and cout << a ? x : y do not mean what they look like; parenthesize those operands.
  • Conditional operator: ?: is right-associative, so a ? b : c ? d : e is a ? b : (c ? d : e).
  • Assignment is right-associative and returns an lvalue, which makes a = b = c work.
  • Three-way comparison <=> sits between shifts and relational operators (C++20).
  • Overloaded operators keep the precedence and associativity of the built-in ones; you cannot change them.