1

If I write the statement

int i=1,2,3;

why does here the comma acts as a separator rather than operator since we have comma operator having associativity from left to right so according to me first value is initialized using i=1 but it doesn't work like this , what's the reason behind this ?

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
angel
  • 47
  • 2

1 Answers1

3

Because the C language grammar says that an initializer must be an assignment-expression. The latter includes all expressions except those formed from two other expressions and the comma operator:

expression :
    assignment-expression
    expression , assignment-expression

So 0,1,2 is not a valid initializer for i. Since 1 is not a valid declarator either, this code does not match any syntax rules, making it a syntax error.

I'd guess that the grammar was designed this way on purpose, to avoid the possibility of any situations where a comma is ambiguous between a separator and an operator.

M.M
  • 138,810
  • 21
  • 208
  • 365