Evaluation order and side effects
Trace full expressions, short-circuit guards, comma operators, function calls, and unsequenced conflicts without guessing an output.
In this chapter8 sections
Knowing how an expression groups does not always tell us which part is evaluated first. Sequencing rules describe the ordering relationships C requires. We will use them to distinguish flexible order from conflicting, invalid accesses.
1. Separate value computation from side effects
A side effect changes execution state, for example by modifying an object or performing I/O. A value computation obtains a value or identifies an object. Post-increment produces the old value and also modifies the object; pre-increment produces the updated value. These descriptions do not grant either operator universal priority over surrounding expressions.
int value = 1;
int before = value++;
int after = ++value;
The initializers here are separate full expressions. The evaluations and side effects of one full expression are sequenced before the next one evaluated. The first increment completes before the later initializer uses value. Keeping dependent updates in separate statements is often the clearest way to express the required order.
2. Know the three ordering relationships
| Relationship | Meaning |
|---|---|
| Sequenced before | A finishes before B begins under the specified relation. |
| Indeterminately sequenced | Either A precedes B or B precedes A, without interleaving, but which order is unspecified. |
| Unsequenced | Neither evaluation is sequenced before the other; do not infer an order. |
Operator precedence and associativity answer how an expression is grouped. Parentheses can clarify that grouping; they do not create sequencing by themselves. For example, grouping additions does not in general decide which operand is evaluated first. A sequencing rule has to come from the language construct or an explicit program step.
3. Use constructs that actually establish sequencing
int divisor = 0;
bool accepted = divisor != 0 && 12 / divisor > 2;
For logical && and ||, the left operand is evaluated first and the right operand is evaluated only when needed. The first operand of ?: is also evaluated before the selected branch, and only one branch is selected. This lets a guard protect a later division or pointer access, provided the guard itself and every other precondition are valid.
int value = 0;
int result = (value = 3, value + 2);
The comma operator sequences its left operand before its right operand and produces the right operand’s value. A comma separating function arguments or declarations is different syntax and supplies no such general ordering guarantee. Parentheses are important here to select the comma-operator expression rather than a declaration separator.
4. Unsequenced conflicts have no valid output to predict
In C17, f(i++, i++) has unsequenced modifications of the same scalar object. i++ + i also combines an unsequenced modification with a value computation of that object. These have undefined behavior. Replacing ++ with a longer assignment or adding parentheses does not automatically fix the ordering. Compute the intended argument values in explicit earlier steps instead.
5. Function executions and argument evaluations are distinct
#include <assert.h>
#include <string.h>
static char trace[3];
static unsigned used;
static int left(void) { trace[used++] = 'L'; return 1; }
static int right(void) { trace[used++] = 'R'; return 2; }
static int add(int first, int second) { return first + second; }
int main(void)
{
int sum = add(left(), right());
trace[used] = '\0';
assert(sum == 3 && used == 2);
assert(strcmp(trace, "LR") == 0 || strcmp(trace, "RL") == 0);
return 0;
}
In this example, left and right are separate function executions whose bodies do not interleave under C17’s sequencing rule for such calls. Their order is unspecified, so either trace is accepted; add receives both completed results. This does not make arbitrary argument expressions such as the two i++ expressions safe. Function bodies, argument computations, and the eventual call are different parts of the evaluation model.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: 5.1.2.3, 6.5p2–p3, 6.5.2.2p10, 6.5.13–6.5.15, 6.5.17, 6.8p4. Stable core rules used for the C17 baseline; not the final ISO publication.
- WG14 N3096 — public C23 working draft: 6.5, 6.5.2.2. C23 notes are source-reviewed separately from C17 example checks.
Before you move on
- Value computation and side effects can have ordering relationships beyond expression grouping.
- Specific operators and full-expression boundaries provide sequencing guarantees.
- Unsequenced conflicting accesses can produce undefined behavior rather than merely multiple possible results.
Keep these ideas separate
- Associativity / Evaluation order
- Associativity groups operators of related precedence; it does not generally prescribe the runtime order of their operands.
When two operations must happen in order, how could separate statements make that requirement clearer?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Both operands always run, in either order.
The right operand is conditionally evaluated, after the left operand.
- The left operand is evaluated first; the right is evaluated only if the left is nonzero.
✓ Correct reasoning. The logical-AND operator provides short-circuit evaluation and sequencing.
- The right operand always runs first.
That reverses this operator’s required evaluation relationship.
C17
Ready for the next idea?
Mark this chapter as read whenever you feel ready.