Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Short-circuit evaluation in C

Reference for short-circuit evaluation in C: when the right operand of && and || is skipped, guard patterns, side effects and result values 0 and 1.

The logical operators && and || evaluate their operands from left to right and stop as soon as the answer is known. For a && b, if a is zero, the result is 0 and b is never evaluated. For a || b, if a is non-zero, the result is 1 and b is never evaluated. This is called short-circuit evaluation and the language guarantees it.

The guarantee is what makes idioms like p != NULL && p->value > 0 safe. It also means that side effects in the right operand do not always happen, which is a classic source of bugs.

C
if (p != NULL && p->count > 0) { /* p is only dereferenced when valid */ }
if (i >= n || data[i] == 0)    { /* data[i] is only read when i is in range */ }

Try it

C Seeing which operands run

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 scanf or fgets), it gets the next line from this box.

C Guards that prevent crashes, and a 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 scanf or fgets), it gets the next line from this box.

Syntax

C
expression1 && expression2
expression1 || expression2
Operator Right operand evaluated when Result
a && b a is non-zero 1 if both are non-zero, otherwise 0
a || b a is zero 1 if either is non-zero, otherwise 0

The result has type int (in C), and a sequence point separates the evaluation of the left operand from the right. The operands may be any scalar type: integers, floating-point values or pointers. A pointer is true when it is not null.

The conditional operator ?: is the third operator with guaranteed partial evaluation: only one of its two branches runs.

Errors / undefined behavior: none of its own. It removes undefined behavior when used as a guard, and the unevaluated operand can be anything, including an expression that would be invalid at run time.

Usage notes

  • Always 0 or 1. 5 && 7 is 1, not 7. This differs from languages like JavaScript and Python where the operand value itself is returned. Do not use it as a "default value" idiom.
  • Order matters. Put the cheap or protective test on the left: p && p->x, never p->x && p.
  • Side effects are conditional. In f() && g(), g() runs only if f() returned non-zero. Do not put required work, such as an increment or an I/O call, in the right operand unless that behavior is intended.
  • Bitwise operators do not short-circuit. & and | always evaluate both operands. a & b and a && b give different results for 1 & 2 (0) and 1 && 2 (1).
  • Mixed chains. && binds tighter than ||. a || b && c is a || (b && c); add parentheses for readers.
  • A skipped operand must still be valid code. It is never run, but the compiler still parses and type-checks it.
  • Optimizers may reorder work only when the result and side effects are unchanged, so short-circuit semantics are always preserved.
  • Comparison chains. 0 < x < 10 is not a range check (it compares (0 < x) with 10). Write 0 < x && x < 10.
  • Macros. In a macro, arguments to &&/|| may not run: another reason to avoid hiding side effects in macro arguments.