Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the order of assignment in a list of initialized variables undefined? [duplicate]

Possible Duplicate:
Is the comma in a variable list a sequence point?

If I have the following code does the comma act as a normal sequence point, or is the behaviour undefined?

int i = 1, j = i;

I don't actually plan to use this (our internal standard prohibits even int i, j) but I was curious and it prooved oddly tricky to google.

like image 785
sji Avatar asked Dec 12 '22 21:12

sji


1 Answers

It's well-defined:

8. Declarators: [dcl.decl]

3) Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

And the note:

90) A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is

T D1, D2, ... Dn;

is usually equvalent to

T D1; T D2; ... T Dn;

where T is a decl-specifier-seq and each Di is an init-declarator.

For completness (because the note says usually):

The exception occurs when a name introduced by one of the declarators hides a type name used by the dcl-specifiers, so that when the same dcl-specifiers are used in a subsequent declaration, they do not have the same meaning, as in struct S { ... }; S S, T; // declare two instances of struct Swhich is not equivalent tostruct S { ... }; S S; S T; // error`

like image 63
Luchian Grigore Avatar answered Feb 15 '23 23:02

Luchian Grigore