YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Correctness & careChapter 59 of 73

Evaluation order and side effects

Trace full expressions, short-circuit guards, comma operators, function calls, and unsequenced conflicts without guessing an output.

5 min readAdvancedC17 baseline
Offline reading
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.

C17 · illustrative fragment
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

RelationshipMeaning
Sequenced beforeA finishes before B begins under the specified relation.
Indeterminately sequencedEither A precedes B or B precedes A, without interleaving, but which order is unspecified.
UnsequencedNeither 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

C17 · illustrative fragment
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.

C17 · illustrative fragment
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

Complete C17 program
#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

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.
Explain it in your own words

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.

What ordering does left && right provide?
Read the explanation for every choice
  1. Both operands always run, in either order.

    The right operand is conditionally evaluated, after the left operand.

  2. 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.

  3. 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.

CHAPTER 59 / 73

In this chapter

Evaluation order and side effects

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping