The C Language

operators, precedence, and associativity

An operator is a symbol that performs an action on values — the + that adds, the == that compares, the = that assigns. Just as in ordinary arithmetic where you learned to do multiplication before addition, C has rules for which operator acts first when several appear in one expression. Those rules are precedence (who goes first) and associativity (the tiebreaker for operators of equal rank: left-to-right or right-to-left).

C has a rich set of operators grouped by job. Arithmetic ones (+ - * / and % for remainder) compute numbers. Relational ones (< > <= >= == !=) compare and yield 0 or 1. Logical ones (&& for and, || for or, ! for not) combine truth values and short-circuit, meaning && stops if the left side is false and || stops if the left side is true. Assignment (= and compound forms like += -=) stores a value and itself produces that value. The comma operator evaluates its left side, discards it, then evaluates and yields its right side. Precedence decides grouping: in a + b * c the multiplication binds tighter, so it is a + (b * c). Associativity breaks ties: a - b - c is left-associative, meaning (a - b) - c, while assignment is right-associative, so a = b = c means a = (b = c).

Why this matters: misjudging precedence is a classic source of bugs, because the code compiles but computes the wrong grouping. The honest advice is not to memorize the entire 15-level table but to add parentheses whenever an expression is not obvious — they cost nothing at run time and make intent unmistakable. Note also that for most operators C does not promise an order in which the two operands are evaluated; relying on that order is unspecified behavior.

int r = 2 + 3 * 4; /* 14, not 20: * binds tighter than + */ int s = (2 + 3) * 4; /* 20: parentheses force the grouping */ int a, b, c; a = b = c = 0; /* right-associative: a = (b = (c = 0)) */

Precedence makes * group before +; parentheses override it. Assignment associates right-to-left, so the chained = reads from the right.

Don't rely on the order operands are evaluated — for most operators it is unspecified, so something like f() + g() may call them in either order. When in doubt about grouping, add parentheses; they are free and remove ambiguity.

Also called
operatorprecedenceassociativity運算子優先順序結合性