Indeterminately sequenced expressions
suggest changeFunction calls as f(a)
always imply a sequence point between the evaluation of the arguments and the designator (here f
and a
) and the actual call. If two such calls are unsequenced, the two function calls are indeterminately sequenced, that is, one is executed before the other, and order is unspecified.
unsigned counter = 0;
unsingned account(void) {
return counter++;
}
int main(void) {
printf("the order is %u %u\n", account(), account());
}
This implicit twofold modification of counter
during the evaluation of the printf
arguments is valid, we just don’t know which of the calls comes first. As the order is unspecified, it may vary and cannot be depended on. So the printout could be:
the order is 0 1
or
the order is 1 0
The analogous statement to the above without intermediate function call
printf("the order is %u %u\n", counter++, counter++); // undefined behavior
has undefined behavior because there is no sequence point between the two modifications of counter
.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents