I recently learned about the , operator and the fact that it introduces a sequence point.
I also learned that the following code led to undefined behavior:
i = ++i;
Because i was modified twice between two sequence points.
But what about the following codes ?
i = 0, ++i;
i = (0, ++i);
While I know the rules, I can’t get to a conclusion. So is it defined behavior or not ?
edit: Just as @paxdiablo mentions, defined or not, this is really a bad practice which should be avoided. This question is asked solely for educational purposes and better understanding of the “rules”.
Yes.
=has higher precedence than,, so this expression is equivalent to(i = 0), ++i.,is a sequence point, so it’s guaranteed that the++ioccurs after the assignment.I’m not sure whether
i = (0, ++i)is defined though. My guess would be no; there’s no sequence point between the increment and the assignment.