C Language Sequence points Indeterminately sequenced expressions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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



Got any C Language Question?