2

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?

4
  • If you are going to vote down the best answer, please provide a better one! Commented Mar 27, 2022 at 13:34
  • In my opinion, it is not enough to consider sequence points, you also have to consider the order in which initializers are evaluated, and as far as I can tell that order is not defined (in C90 at least). By the way, if you added the "language-lawyer" tag to your question, you might attract the attention of people more qualified, than me, to answer this question. Commented Mar 27, 2022 at 17:10
  • Thanks. Based on what do you see that in C89 the order in which initializers are evaluated is not defined? In fact, KamilCuk settled the question for all standards C99 and later by pointing out that: "The order in which any side effects occur among the initialization list expressions is unspecified." (see here port70.net/~nsz/c/c99/n1256.html#6.7.8p23 and here port70.net/~nsz/c/c99/n1256.html#note133) But this is missing from the C90 standard. Commented Mar 27, 2022 at 17:29
  • But this is missing from the C90 standard If it's missing, means it is undefined. Commented Mar 27, 2022 at 21:23

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

KamilCuk, thanks for the quick reply. It is straight from here: port70.net/%7Ensz/c/c11/n1570.html#C ..."Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions: an initializer that is not part of a compound literal..." and port70.net/%7Ensz/c/c11/n1570.html#6.7.9. The line that you quoted isn't there in older versions of the standard.
an initializer that is not part of a compound literal This is an initializer, as a whole, not initializer-list expression. Yes. int a[2] = ...; <here> there is a sequence point. This is so that int a = i + 1, b = ++i is defined. This part of AnnexC is about port70.net/%7Ensz/c/c11/n1570.html#6.8p4 , and there is a sequence point after "full expression" - the initialization, as a whole. It tells nothing about the relation of "initializer-list expressions" inside (or "during") initializer itself.
Yes, but the initializer list consists of comma separated initializers. That precisely was my question.... or rather what I find not clear. i = 1 and ++i are both going to be parsed as initializers according to the grammar rule that is used to define it in the standard.
Och, that's right, I see! So there are sequence points after these "comma separated smaller initializers", but they are anyway indeterminately sequenced with each other ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.