-3

I have this C code line:

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

printf("%d", a);

Why the value 3 is printed? (the last one).

mariusmmg2
  • 713
  • 18
  • 37
  • 3
    I believe Google is not down currently – why don't you consult it for some beginners' tutorials or language references in which operators are explained? – The Paramagnetic Croissant Nov 04 '15 at 17:08
  • 2
    This is a perfectly legitimate newbie question. – Johann Gerell Nov 04 '15 at 17:10
  • 1
    see [What does the comma operator `,` do in C?](http://stackoverflow.com/q/52550/1708801) – Shafik Yaghmour Nov 04 '15 at 17:15
  • 1
    @JohannGerell To wonder, yes. To ask here without any sort of basic searching, no. That's pretty much the main thing given as a valid reason for down voting. –  Nov 04 '15 at 17:39
  • @hvd I doubt that many newbies can successfully formulate a Google search term for that unless they already know that ',' (comma) is an _operator_ in C, and if they did know that, the need to ask at all would diminish. Or how would _you_ do that search? – Johann Gerell Nov 05 '15 at 12:50
  • 1
    @JohannGerell I would search for "what does a comma-separated list of numbers mean in c". And that does give an answered question in its results. Actually, the question you find in the results was answered by me, but I promise I didn't go through my own posting history to guess search terms that might work. :) –  Nov 05 '15 at 13:11

1 Answers1

2

The comma operator evaluates all its "members" but returns the value of the last expression.
From the C11 standard:

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67