Operator Precedence in C++
Precedence decides which operator grabs its operands first when parentheses are absent: * and / before + and -, comparisons before && before ||, and assignment nearly last. C++ has over a dozen precedence levels (plus left/right associativity deciding ties), and nobody memorizes all of them reliably.
The professional rule is simple: know the big five (! and unary first, then *//%, then +-, then comparisons, then logicals, then assignment last) and parenthesize everything else. Explicit parentheses cost nothing at run time and convert precedence puzzles into obviously-correct code.
Arithmetic first
2 + 3 * 4 is 14, not 20 — multiplicative operators bind tighter than additive ones, exactly like school math.
Comparisons before logicals
a < b && c < d groups as (a < b) && (c < d) — relational operators outrank && and ||, so range tests read naturally.
Assignment binds loosest
x = a + b means x = (a + b), never (x = a) + b — assignment happens after nearly everything else on its right.
When in doubt, parenthesize
Shifts vs arithmetic (a << b + 1 is a << (b+1)), ternaries, and bitwise-vs-comparison mixes (x & 1 == 1 is x & (1==1)) are classic traps parentheses erase.
#include <iostream> int main() { int r1 = 2 + 3 * 4; bool r2 = 5 > 3 && 2 > 4; int r3 = (2 + 3) * 4; std::cout << r1 << ", " << r2 << ", " << r3 << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.