+ 11

What is this?

int x=2,y=1,z=2; int c=(y,x,z); cout<<c; There was a challenge q that looked like that. What is (y,x,z)? Never seen that before.

27th Mar 2017, 12:59 AM
Karl T.
Karl T. - avatar
2 Answers
+ 7
That's the comma operator. When you see "(x, y, z)", it evaluates to the last item in the list. It's useful if you need to evaluate a statement before checking a condition, like "(statement, condition)". Don't forget about operator precedence, though! The parentheses are important here. If it was "c=x,y,z", c would then be equal to x rather than z, because assignment has higher precedence than the comma operator.
27th Mar 2017, 1:08 AM
Squidy
Squidy - avatar
+ 8
Cool thanks.
27th Mar 2017, 1:09 AM
Karl T.
Karl T. - avatar