"Int i = x ++, j = x ++;" legally?

Pretty clear in the title, I think. I'm not quite sure about this, and I cannot find a good answer through Googles (alas, I am not committed to the fine art of standards-fu), so I ask:

int i = x++, j = x++;

Is it determined? I am sure that the i = x++, j = x++; normal operator will be undefined behavior - this is a comma operator, which is a sequence point and will be legal, but without a source it is clear enough whether the initializer ends with a semicolon or when the next variable is declared, and since it is not used by a comma, I can not find a clear answer. Thus, either: a) the comma ends the initializer, is a point in the sequence, and this works, or b) it is not. What is it?

And to rule out, I know that I have to simplify the headache and just write it as:

int i = x++;
int j = x++;

And ensure that it is defined. I ask more out of curiosity.

+5
source share
2 answers

The end of the initializer is a sequence point, so the example in the header is legal.

The comma operator is also a sequence point, so your “normal statement” is also legal and well-defined.

The wikipedia article has a list of C and C ++ sequence points.

To follow the comment below, it demonstrates the terrible power of the comma operator stored in FreeBSD stdio.h (under ifndef __GNUC__):

/*
 * This has been tuned to generate reasonable code on the vax using pcc.
 */
#define __sputc(c, p) \
        (--(p)->_w < 0 ? \
                (p)->_w >= (p)->_lbfsize ? \
                        (*(p)->_p = (c)), *(p)->_p != '\n' ? \
                                (int)*(p)->_p++ : \
                                __swbuf('\n', p) : \
                        __swbuf((int)(c), p) : \
                (*(p)->_p = (c), (int)*(p)->_p++))
#endif
+11

. :

int i = 0, j = 0, x = 10;
i = x++, j = x++;

: undefined. ( C ++ )

: D ofc , undefined, ...

-5

All Articles