LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

What is C operator precedence and why does it matter?

Precedence decides which operator binds first when parentheses are absent (higher = applied first); get it wrong and an expression silently parses into something you didn't mean.

Precedence Operator Description Associativity
1 (highest) ++ -- () [] . -> Postfix, call, access Left-to-right
2 + - ! ~ (type) * & Unary, cast, deref, addr Right-to-left
3 * / % Multiplication, division Left-to-right
4 + - Addition, subtraction Left-to-right
5 << >> Bit shifts Left-to-right
6 < <= > >= Relational Left-to-right
7 == != Equality Left-to-right
8 & Bitwise AND Left-to-right
9 ^ Bitwise XOR Left-to-right
10 | Bitwise OR Left-to-right
11 && Logical AND Left-to-right
12 || Logical OR Left-to-right
13 ?: Ternary Right-to-left
14 = += -= etc. Assignment Right-to-left
15 (lowest) , Comma Left-to-right

Why it matters:

d = *p & a ^ (b << (c!=d));
// Without knowing precedence, this is impossible to parse!
// Parsed as: d = ((*p) & a) ^ (b << (c != d));

Common gotcha: Bitwise & has LOWER precedence than ==:

// WRONG: parsed as flags & (MASK == 0)
if (flags & MASK == 0)
// CORRECT
if ((flags & MASK) == 0)

Tip: When in doubt, use parentheses! See: https://en.cppreference.com/w/c/language/operator_precedence

From Quiz: REVE1 / C Programming | Updated: Jul 14, 2026