Home > front end >  Initializer lists in C and sequence points
Initializer lists in C and sequence points

Time:03-28

The C Standard states that there is a sequence point at the end of a full expression in an initializer and that

initializer:

        assignment-expression

        { initializer-list }

        { initializer-list , }

initializer-list:

        initializer

        initializer-list , initializer

That would mean, however, that this

int a[2] = { i = 1 ,   i };

ought to be fine. Could someone please explain why, or why not, this is the case?

CodePudding user response:

I do not know where you see that. I see https://port70.net/~nsz/c/c11/n1570.html#6.7.9p23 :

The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified.

ought to be fine. Could someone please explain why

It is "fine", as in the behavior is defined to be unspecified behavior. You do not know, which one of i = 1 or i will execute first or last, one of them will.

  • Related