1

Possible Duplicate:
C comma operator

I came across a line of code which I couldn't understand. I remember seeing something similar somewhere.

int x,y,z;
x=(y=2,z=2*y,z+4);

I know that the value assigned to x is 8. Can someone explain me why?

Community
  • 1
  • 1
William J.
  • 1,574
  • 15
  • 26

2 Answers2

4

This is equivalent to:

 y = 2;      // y == 2
 z = 2 * y;  // z == 4
 x = z + 4;  // x == 8

The operands of the comma operator are evaluated from left to right and the result is the value of the right operand.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Is there a special name for this type of assignment ? – asheeshr Dec 31 '12 at 12:04
  • 6
    @AshRj yes, "dontdoit assignment" – effeffe Dec 31 '12 at 12:05
  • 1
    Yes, there is a name for it "Rubbish!" – Mats Petersson Dec 31 '12 at 12:05
  • I ask because i have never seen this in any C books that i refer/use. Whats the problem in doing this ? – asheeshr Dec 31 '12 at 12:06
  • @AshRj: Because it is not a good practice to use it. That simply doesn't make it invalid. The problem is same as you faced. it is not readable! – Alok Save Dec 31 '12 at 12:07
  • @AlokSave Its not good practise because people dont know about it, or because there are some hidden dangers lurking behind it ? – asheeshr Dec 31 '12 at 12:09
  • It is a good idea NOT to use this, because like you, most people won't understand what it does [and it's clearly not logical or useful in any way]. Code should be easy to understand. – Mats Petersson Dec 31 '12 at 12:12
  • Because it is clever by half. In C it's "not so bad" i.e. merely confusing the first time you see it. But in C++ people (naturally) want to turn "not so bad" to "not so bad++" i.e. god-awful obfuscated code through the magic of operator overloading. Google "comma" and operator overloading. Still, that might just be considered a reason why C++ is to be avoided -- not necessarily the fault of the comma. – user268396 Dec 31 '12 at 12:14
  • And if you think "Well, if it's hard to understand, no one will steal my code", then you have to consider that if it's actually worth protecting [that is, given the idea of WHAT you do in the first place, would it really be the code, or the idea, that is valuable], would someone put a lot of effort in taking your code, or just writing their own from scratch. Unreadable code would definitely not help if you ever came to sell your idea to a commercial entity. – Mats Petersson Dec 31 '12 at 12:14
  • @AshRj because there is no reason to do it and it obfuscates the code, do only one thing on each line. Or at least a small set or related operations, like `++i, ++j;`. Comma operator is useful sometimes, in `for` loops for example. – effeffe Dec 31 '12 at 12:14
  • @effeffe: The key here is to not use assignment from a set of comma operator separated expressions, as it rarely does anything useful anyways. Sure, doing to increments, or `q = p, p = p->next` on a for-loop line is fine. – Mats Petersson Dec 31 '12 at 12:18
  • Quite a few reasons against using it. – asheeshr Dec 31 '12 at 12:24
  • @AshRj without any good reason to do it, even one reason against it it's enough to avoid it. – effeffe Dec 31 '12 at 12:50
  • @Oli It was a question I found in a test, maybe that's why they used this odd assignment. Thank you all for the answers and comments. – William J. Dec 31 '12 at 12:57
0

the comma operator separates the previous values, and the last item in the comma is returned as the result, e.g.

a = b,c 

assings the value of c to a. The parentheses here do essentially nothing, btw

So you have two assignments, then a statement, whose result is returned and assigned to x

bjorke
  • 3,295
  • 1
  • 16
  • 20