I know that things like x = x++ + ++x
invokes undefined behavior because a variable is modified multiple times within the same sequence point. That's thoroughly explained in this post Why are these constructs using pre and post-increment undefined behavior?
But consider a thing like printf("foo") + printf("bar")
. The function printf
returns an int
, so the expression is valid in that sense. But the order of evaluation for the +
operator is not specified in the standard, so it is not clear if this will print foobar
or barfoo
.
But my question here is if this also is undefined behavior.
printf("foo") + printf("bar")
does not have undefined behavior (except for the caveat noted below) because the function calls are indeterminately sequenced and are not unsequenced.C effectively has three possibilities for sequencing:
To distinguish between the latter two, suppose writing to
stdout
requires putting bytes in a buffer and updating the counter of how many bytes are in the buffer. (For this, we will neglect what happens when the buffer is full or should be sent to the output device.) Consider two writes tostdout
, called A and B.If A and B are indeterminately sequenced, then either one can go first, but both of its parts—writing the bytes and updating the counter—must be completed before the other one starts. If A and B are unsequenced, then nothing controls the parts; we might have: A puts its bytes in the buffer, B puts its bytes in the buffer, A updates the counter, B updates the counter.
In the former case, both writes are completed, but they can be completed in either order. In the latter case, the behavior is undefined. One of the possibilities is that B writes its bytes in the same place in the buffer as A’s bytes, losing A's bytes, because the counter was not updated to tell B where its new bytes should go.
In
printf("foo") + printf("bar")
, the writes tostdout
are indeterminately sequenced. This is because the function calls provide sequence points that separate the side effects, but we do not know in which order they are evaluated.C 2018 6.5.2.2 10 tells us that function calls introduce sequence points:
Thus, if the C implementation happens to evaluate
printf("foo")
second, there is a sequence point just before the actual call, and the evaluation ofprintf("bar")
must have been sequenced before this. Conversely, if the implementation evaluatesprintf("bar")
first, thenprintf("foo")
must have been sequenced before it. So, there is sequencing, albeit indeterminate.Additionally, 7.1.4 3 tells us:
Therefore, the two function calls are indeterminately sequenced. The rule in 6.5 2 about unsequenced side effects does not apply:
(Not to mention the fact that
stdout
is not a scalar object.)Caveat
There is a hazard that the C standard permits standard library functions to be implemented as function-like macros (C 2018 7.1.4 1). In this case, the reasoning above about sequence points might not apply. A program can force function calls by enclosing the name in parentheses so that it will not be treated as an invocation of a function-like macro:
(printf)("foo") + (printf)("bar")
.