PROGRAMMING IN C/Student Engineering Notebook
Page 01·notes
OPERATOR PRECEDENCE, SHORT-CIRCUIT & STORAGE CLASSES—~30 MIN READ

Operator Precedence, Associativity & Short-Circuit Evaluation

“Prefix vs postfix increments, bitwise vs logical operators, and short-circuit traps.”

1.The C Operator Precedence & Associativity Hierarchy

Expressions in C are evaluated according to a strict priority hierarchy. When operators have equal precedence, associativity dictates the direction of binding (Left-to-Right or Right-to-Left).

Complete Precedence Tiers (Highest to Lowest):

1. Postfix Operators (L-to-R): Array subscript [], Function call (), Member selection . and ->, Postfix increment/decrement x++, x--.

2. Unary Operators (RIGHT-TO-LEFT!): Prefix increment/decrement ++x, --x, Unary plus/minus +x, -x, Logical NOT !, Bitwise NOT ~, Type cast (type), Dereference *p, Address-of &x, sizeof.

3. Multiplicative (L-to-R): *, /, %.

4. Additive (L-to-R): +, -.

5. Bitwise Shift (L-to-R): <<, >>.

6. Relational (L-to-R): <, <=, >, >=.

7. Equality (L-to-R): ==, !=.

8. Bitwise AND (L-to-R): &.

9. Bitwise XOR (L-to-R): ^.

10. Bitwise OR (L-to-R): |.

11. Logical AND (L-to-R): && (has sequence point).

12. Logical OR (L-to-R): || (has sequence point).

13. Ternary Conditional (RIGHT-TO-LEFT!): ? :.

14. Assignment (RIGHT-TO-LEFT!): =, +=, -=, *=, /=, %=, etc.

15. Comma Operator (Lowest, L-to-R): , (evaluates left operand, discards it, returns right operand).

notice this:— Right-to-Left Associativity Trap

Notice that Unary operators, Ternary (? :), and Assignment (=) associate RIGHT-TO-LEFT! For example, a = b = c = 5 evaluates as a = (b = (c = 5)): assigns 5 to c, then b, then a. Similarly, p++ groups as (p++): dereferences the OLD pointer value, then increments the pointer address!

2.Short-Circuit Evaluation in Logical Expressions

C compilers guarantee short-circuit evaluation for logical AND (&&) and logical OR (||) strictly from left to right:

1. For expr1 && expr2: If expr1 evaluates to 0 (FALSE), expr2 is NEVER evaluated, because the total expression is guaranteed to be 0.

2. For expr1 || expr2: If expr1 evaluates to non-zero (TRUE), expr2 is NEVER evaluated, because the total expression is guaranteed to be 1.

Sequence Points: The logical &&, logical ||, ternary ? :, and comma , operators introduce a sequence point: all side effects of the left operand are fully committed before the right operand is accessed.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION2 Marks (Classical GATE Snippet)

3. GATE Worked Code Tracing: Short-Circuit Side Effects

⚠️ Pitfall Warning:

Never skip short-circuit evaluation! If a pre/post-increment is inside a bypassed expression, the variable's value DOES NOT change!

Consider the following C code snippet:

int a = 1, b = 0, c = 2, result;

result = a-- || ++b && ++c;

What are the final values of a, b, c, and result?

Step 1: Parse expression precedence:

Precedence of && is higher than ||, so the expression groups as: (a--) || (++b && ++c).

Step 2: Evaluate left operand of ||: '(a--)':

The current value of 'a' (1) is tested. Since 1 != 0 (TRUE), the left operand of || is TRUE!

Post-decrement side effect occurs: 'a' becomes 0.

Step 3: Apply short-circuit rule for ||:

Because the left operand evaluated to TRUE, the entire right operand '(++b && ++c)' is completely BYPASSED and NEVER evaluated!

'++b' is NOT executed -> b remains 0.

'++c' is NOT executed -> c remains 2.

'result' evaluates to 1 (TRUE).

Final State: a = 0, b = 0, c = 2, result = 1.

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
The C Programming Language (2nd Edition, ANSI C) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Neso Academy · C Programming Full Course↗Instructor: Neso Academy · Covers: Data types, pointers, arrays, functions, recursion, file I/O

Systematic beginner-to-advanced C with clear examples.

Open YouTube↗
Jenny's Lectures CS/IT · C Programming↗Instructor: Jenny's Lectures · Covers: Arrays, strings, pointers, structures, file handling, preprocessor

Clear step-by-step C explanations — great for semester lab preparation.

Open YouTube↗