Indeterminately sequenced expressions

suggest change

A function call such as f(a) has a sequence point after the function designator and the arguments are evaluated and before the actual call. The designator and the arguments are unsequenced relative to each other. If two such calls are unsequenced, the calls themselves are indeterminately sequenced: one finishes before the other starts, and which one is first is unspecified.

#include <stdio.h>

unsigned counter = 0;

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents