#include<stdio.h>
int main(void) {
int a;
a = (1, 2), 3;
printf("%d", a);
return 0;
}
output: 2
Can any one explain how output is 2?
Can any one explain how output is 2?
In the statement
a = (1, 2), 3;
,
used is a comma operator.
Due to higher operator precedence of =
operator than that of ,
operator, the expression operand (1, 2)
will bind to =
as
(a = (1, 2)), 3;
In case of comma operator, the left operand of a comma operator is evaluated to a void expression, then the right operand is evaluated and the result has the value and type of the right operand.
There are two comma operators here. For the first comma operator in the expression (1, 2)
, 1
will be evaluated to void expression and then 2
will be evaluated and will be assigned to a
.
Now side effect to a
has been taken place and therefore the right operand of second comma operator 3
will be evaluated and the value of the expression (a = (1, 2)), 3
will be 3
.