Search code examples
c++ccomma-operator

What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3);


I've just come across code that essentially does the following:

int a = (1, 2, 3);

I've never seen this notation before. What does it mean?


Solution

  • This is the comma operator: evaluation of a, b first causes a to be evaluated, then b, and the result is that of b.

    int a = (1, 2, 3); first evaluates 1, then 2, finally 3, and uses that last 3 to initialise a. It is useless here, but it can be useful when the left operand of , has side effects (usually: when it's a function call).