I am looking for some explanations regarding the behavior of the following code:
int a = 1;
int b = (4,5,6);
cout<<a+b;
Why is it that b takes the last value 6?
EDIT: Sorry, I made the wrong example, thanks to all for the explaining how b gets the value 6.
I’m sorry, your code is not syntactically valid.
2and3should be declaration names. Likecandd.To use the comma operator, you have to parenthesize
1, 2, 3.I will forgive you this accident and continue to explain.
If you use the comma operator, which is used as
operand1 , operand2, it will give you the value (or lvalue) ofoperand2. So you initializebto6(as in(4, 5), 6, since the comma operator is left associative).