Compiled using GCC on a Fedora 20 Desktop, the following code outputs 10.
int x=10;
int y=5;
printf("%d",(y,x));
Compiled using GCC on a Fedora 20 Desktop, the following code outputs 10.
int x=10;
int y=5;
printf("%d",(y,x));
In C, (y,x)
means evaluating x
and y
and returning only x
. EG. From wikipedia:
the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
That´s the "comma operator" (not to be confused
with the comma when separating function parameters.)
The "result" is only the last part, but if the other parts are functions etc.,
they will be executed too.
In C (exp1,exp2)
First exp1
is evaluated, then exp2
is evaluated, and the value of exp2
is returned for the whole expression.
(exp1, exp2)
is like (exp1 && exp2)
but both exp1
and exp2
will
always be evaluated, whatever exp1
returns.(exp1, exp2)
is like { exp1; exp2; }
but it can be used as an
expression in a function call or assignment.