tags:

views:

2717

answers:

7
+13  Q: 

C++ Comma Operator

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;
+16  A: 

It would be equal to b.

The comma operator has a lower precedence than assignment.

Leon Timmermans
+1  A: 

Please see this question because this topic has already been covered. See http://en.wikipedia.org/wiki/Comma_operator for more information.

Ryan Lanciaux
Well no. This is a hidden operator precedence puzzler.
Tom Hawtin - tackline
Don't confuse C with C++. The same answer isn't the whole truth here.
Konrad Rudolph
If the asker intends it to be a puzzle, it should be marked as such in the question. Otherwise people will think this is just a basic C syntax/semantics question.
Kristopher Johnson
Confusing C w/ C++ ? Did you not read the wikipedia article? It applies to both.
Ryan Lanciaux
I think this question is more specific, focusing on the implications of the comma operator on assignment.
Steve Duitsman
@Ryan: The , operator can be overloaded in C++, which isn't an issue in C.
Joel
+3  A: 

b's value will be assigned to a. Nothing will happen to c

Prakash
+17  A: 

Take care to notice that the comma operator may be overloaded in C++. The actual behaviour may thus be very different from the one expected.

As an example, Boost.Spirit uses the comma operator quite cleverly to implement list initializers for symbol tables. Thus, it makes the following syntax possible and meaningful:

keywords = "and", "or", "not", "xor";
Konrad Rudolph
+1  A: 

The value of a will be equal to b, since the comma operator has a lower precedence the assignment operator.

Jason Carreiro
+1  A: 

The value of a will be b, but the value of the statement will be c. That is, in

d = (a = b, c);

a would be equal to b, and d would be equal to c

MobyDX
Almost correct. Statements don't have values, expressions do. The value of that expression is c.
Leon Timmermans
+13  A: 

The comma operator has the lowest precedence of all C/C++ operators. Therefore it's always the last one to bind to an expression, meaning this:

a = b, c;

is equivalent to:

(a = b), c;

Another interesting fact is that the comma operator introduces a sequence point. This means that the expression:

a+b, c(), d

is guaranteed to have its three subexpressions (a+b, c() and d) evaluated in order. This is significant if they have side-effects. Normally compilers are allowed to evaluate subexpressions in whatever order they find fit; for example, in a function call:

someFunc(arg1, arg2, arg3)

arguments can be evaluated in an arbitrary order. Note that the commas in the function call are not operators; they are separators.

efotinis