Why is a assigned the value 3? Does the compiler simply take the last value from the list?
int a;
a=(1,2,3);
printf("%d",a);
How does compiler parse this statement or how it works internally?
Why is a assigned the value 3? Does the compiler simply take the last value from the list?
int a;
a=(1,2,3);
printf("%d",a);
How does compiler parse this statement or how it works internally?
Comma in (1,2,3)
is a comma operator. It is evaluated as
a = ( (1,2) ,3 );
Comma operator is left associative. The result/value of the expression (1,2,3)
is the value of the right operand of comma operator.
As pointed out in the comments, it's because you're using the comma operator. That means the 1 and 2 are evaluated and discarded. The three is the only thing left to be assigned. Without the parenthesis it would most likely be assigned as 1.