x = 1, 2, 3;
Due to precedence, this is interpreted as (x = 1), 2, 3;
.
int x = 1, 2, 3;
In an expression statement such as x = 1, 2, 3;
, you can use any expression. However, in a declaration, an initializer can only be an assignment-expression (or a list of initializers inside {
and }
).
The way the formal C grammar is written, the top-level operator of an expression can be any of the C operators, but the top-level operator of an assignment-expression can be any of the C operators except ,
. Therefore, if a ,
is seen when parsing the top level of an initializer, it cannot be part of the initializer, so it ends the initializer. (A ,
can appears inside parentheses in an initializer.)
Here are some details about how this works. The formal grammar says:
- An expression is either expression
,
assignment-expression or is an assignment-expression.
- An assignment-expression is either unary-expression assignment-operator assignment-expression or is a conditional-expression.
- A conditional-expression is either logical-OR-expression
?
expression :
conditional-expression or is a logical-OR-expression.
- This chain continues through all the operators in C.
So, when a C compiler is looking for an expression, it notionally goes through each of these choices, accepting either a comma operator or an assignment operator or a conditional operator, and so on. When it is looking for an assignment-expression (in a declaration), it starts with the second choice, an assignment operator, notionally skipping over the comma as a possibility.