tags:

views:

189

answers:

3

I saw this question been asked at SO few hours ago.

My question is related to it but just a bit different and is a result of Johannes Schaub-litb's comment to Oli Charlesworth's answer.

Is i = (0, ++i, 0) undefined behavior?

P.S: This is just for educational purpose and has no relation with real life code or examples. Please ignore this question if you just want to add comments like "Why do you care? Nobody uses it in real world applications." etc

TIA

+1  A: 

If i = (++i, 0) is defined, then so is this. Perhaps you should simplify the question!

Oli Charlesworth
Unfortunately that is not undefined. But the other way around is `i = (0,++i)`
Martin York
+10  A: 

No, it is not undefined behavior.

The only potential for undefined behavior here is the multiple modifications of i, but the one inside the () is isolated from the other by sequence points inherent in , operator. So, no undefined behavior.

AndreyT
+2  A: 

I think that in i = (0, ++i, 0), i is modified at different sequence points, because of the comma operator, so therefore the behavior is defined.

Michael Goldshteyn
There are two sequence points here (both the `,`). The second one ought to occur before the assignment, so I believe this is defined.
Oli Charlesworth
Yes, the comma does indicate a sequence point...
Michael Goldshteyn
...unless the type of i is user-defined
sellibitze