The comma sequence operator introduces a sequence point in an expression. I am wondering whether this means that the program below avoids undefined behavior.
int x, y;
int main()
{
return (x++, y) + (y++, x);
}
If it does avoid undefined behavior, it could still be unspecified, that is, return one of several possible values. I would think that in C99, it can only compute 1
, but actually, various versions of GCC compile this program into an executable that returns 2
. Clang generates an executable that returns 1
, apparently agreeing with my intuition.
Lastly, is this something that changed in C11?
+
are evaluated in parallel, so to speak. – Threecornered+
are evaluated in parallel” explanation not apply tof() + g()
(which the consensus is that it is not undefined #3951517 )? – Marileef()
whenf()
returns astruct
. For what it's worth, GCC compiles(x++, y+1) + (y++, x+1)
into4
, and surely that cannot be excused by this sentence. – Marileee1 + e2
, are the evaluations ofe1
ande2
unsequenced or indeterminately sequenced? If unsequenced, it's undefined behaviour, if indeterminately sequenced, merely unspecified. – Obviate(0, x++, y+1) + (0, y++, x+1)
? (which GCC still evaluates as 4) – Marilee