# Comma as a separator and operator

• A+
Category：Languages

So I came across this question somewhere:

Case 1:

``int a; a = 1, 2, 3; printf("%d", a); ``

Case 2:

`` int a = 1, 2, 3;  printf("%d", a); ``

The explanation says:

The first case gives error because comma is used as a separator, In second case `=` takes precedence over `,` so it is basically `(a=1), 2, 3`;

But I want to ask why does `=` not take precedence over `,` in Case 1?

This

``int a = 1, 2, 3;/* not a valid one */ ``

is wrong because since `=` has higher priority, so it become `int a = 1` internally and there is no name for `2` and `3` thats why this statement is not valid and cause compile time error.

To avoid this you might want to use

``int a = (1, 2, 3); /* evaluate all expression inside () from L->R and assign right most expression to a i.e a=3*/ ``

And here

``int a; a = 1,2,3;  ``

there are two operator `=` and `,` and see `man operator`. The assignment operator `=` has higher priority than `comma` operator. So it becomes `a=1`.

``a = 1,2,3;     | L--->R(coma operator associativity)      this got assigned to a ``

for e.g

``int x = 10, y = 20,z; z = 100,200,y=30,0; /* solve all expression form L to R, but finally it becomes z=100*/  printf("x = %d y = %d z = %d/n",x,y,z);/* x = 10, y = 30(not 20) z = 100 */ z = (100,200,y=30,0); /* solve all expression form L to R, but assign right most expression value to z*/  ``