Can anybody explain for me:
int a, b, c, d;
a = 2;
b = 4;
c = a, b;
d = (a, b);
Why c == 2
and d == 4
???
Can anybody explain for me:
int a, b, c, d;
a = 2;
b = 4;
c = a, b;
d = (a, b);
Why c == 2
and d == 4
???
The two statements are both evaluated as
c = a;
d = b;
due to how the comma operator (which has the lowest precedence of any operator) works in C and C++.
For the first one, c = a
is evaluated first (as =
has higher precedence than the comma operator) then b
(which is a no-op) is evaluated. The entire expression has a value b
but that's not assigned to anything.
For d = (a, b);
, (a, b)
is first evaluated due to the parentheses. This has a value b
, and that is assigned to d
.