c - Is writing 3 instructions separated by comma `,` undefined behaviour? -
i think saw somewhere writing more 1 instruction separated comma ,
undefined behavior.
so following code generate undefined behavior?
for (i=0, j=3, k=1; i<3 && j<9 && k<5; i++, j++, k++) { printf("%d %d %d\n", i, j, k); }
because there 3 instructions separated comma ,
:
i++, j++, k++
writing more 1 instruction separated comma , undefined behaviour.
nope, it's not general case.
in case, i++, j++, k++
valid.
fwiw, per c11
, chapter §6.5.17, comma operator (emphasis mine)
the left operand of comma operator evaluated void expression; there sequence point between evaluation , of right operand. right operand evaluated; [...]
[note]: might have got confused seeing along line of
printf("%d %d %d", i++, ++i, i);
kind of statement, note, there ,
not comma operator altogether (rather, separator supplied arguments) , sequencing not happen. so, kind of statements are ub.
again, referring standard, footnote 3 same chapter
as indicated syntax, comma operator (as described in subclause) cannot appear in contexts comma used separate items in list (such arguments functions or lists of initializers).
Comments
Post a Comment