-1

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?

2 Answers2

1

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.

haccks
  • 100,941
  • 24
  • 163
  • 252
0

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.

Sunjay Varma
  • 4,735
  • 6
  • 33
  • 50