10
int a = 1;
int b = (1,2,3);
cout << a+b << endl; // this prints 4
  1. Is (1,2,3) some sort of structure in c++ (some primitive type of list, maybe?)
  2. Why is b assigned the value 3? Does the compiler simply take the last value from the list?
Joe McGrath
  • 1,481
  • 10
  • 26
conectionist
  • 2,694
  • 6
  • 28
  • 50
  • 1
    There is no logic behind that code. It is something a teacher would make up to test your understanding of the comma operator. Use the [homework] when appropriate. – Hans Passant Dec 23 '11 at 20:54
  • or is a typo bug. Ie (1,2,3) is not what the author of the code intended. – ddyer Dec 25 '11 at 18:19
  • possible duplicate of [What does the comma operator \`,\` do in C?](http://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c) – Mohit Jain Jul 09 '15 at 13:04

5 Answers5

8

Yes, that's exactly it: the compiler takes the last value. That's the comma operator, and it evaluates its operands left-to-right and returns the rightmost one. It also resolves left-to-right. Why anyone would write code like that, I have no idea :)

So int b = (1, 2, 3) is equivalent to int b = 3. It's not a primitive list of any kind, and the comma operator , is mostly used to evaluate multiple commands in the context of one expression, like a += 5, b += 4, c += 3, d += 2, e += 1, f for example.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @LokiAstari: Yup. (I just realized - by "Why anyone would write code like that, I have no idea" I was referring to `(1, 2, 3)` which is useless. The comma operator is okay in other cases `:)`) – Ry- Dec 23 '11 at 20:55
  • The only possible (not necessarily good) reason I can think of would be stringing a bunch of operations together where you don't care about the results of each. [example](http://ideone.com/MuwxQ). *edit:* and Loki's would be another. – Brigand Dec 23 '11 at 20:56
  • 1
    @FakeRainBrigand: That 4 looks more like 630 to me :) – Ry- Dec 23 '11 at 20:57
  • @FakeRainBrigand: Why not use ';' instead then? – Martin York Dec 23 '11 at 20:58
  • @LokiAstari: It's just an example. `a * a * a * a` would be a lot better :) – Ry- Dec 23 '11 at 20:59
  • Of course you could, but in our hypothetical program `c` is a kind of working storage. We only care about it because it calculates `b`. I don't think there's any situation where you absolutely *need* this syntax. – Brigand Dec 23 '11 at 21:04
6

(1,2,3) is an expression using two instances of the comma operator. The comma operator evaluates its left operand, then there's a sequence point and then it evaluates its right operand. The value of the comma operator is the result of the evaluation of the right hand operand, the result of evaluating the left operand is discarded.

int b = (1,2,3);

is, therefore, equivalent to:

int b = 3;

Most compilers will warn about such a use of the comma operand as there is only ever a point to using a comma operator if the left hand expression has some side effect.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
4

The second line is using the comma operator. An expression like

a, b

evaluates both a and b and returns b.

In this case, the second line is parsed like:

int b = ((1, 2), 3);

so (1, 2) is evaluated (to 2) then thrown away, and the end result is simply 3.

Paolo Capriotti
  • 4,052
  • 21
  • 25
1

It is the comma operator which takes the form expr, expr. The first expression is evaluated and the result is discarded, the second expression is evaluated and its result is returned.

In your case that line is evaluated as:

((1, 2), 3) =>
(2, 3) =>
3
Dave Rager
  • 8,002
  • 3
  • 33
  • 52
-3

it probably used to be b = foo(1,2,3) but the foo got deleted accidentally. C doesn't complain about this kind of crap "it's a feature".

ddyer
  • 1,792
  • 19
  • 26
  • I think those of you who are thumbing down this answer are missing an important point. No one would write this code. If the questioner is trying to understand this code, he must understand that it is not what the author actually intended either. – ddyer Dec 25 '11 at 18:15