How does the comma operator work, and what precedence does it have?

c++, comma-operator, operator-precedence

Solution

It would be equal to `b`.

The comma operator has a lower precedence than assignment.

Problem

How does the comma operator work in C++? For instance, if I do: ``` a = b, c; ``` Does a end up equaling b or c? (Yes, I know this is easy to test - just documenting on here for someone to find the answer quickly.) Update: This question has exposed a nuance when using the comma operator. Just to document this: ``` a = b, c; // a is set to the value of b! a = (b, c); // a is set to the value of c! ``` This question was actually inspired by a typo in code. What was intended to be ``` a = b; c = d; ``` Turned into ``` a = b, // <- Note comma typo! c = d; ```

Original source

Related problems