tags:

views:

464

answers:

4

What does this piece of code in C do:

p = (1, 2.1);

What do we know about p?

+17  A: 

The comma operator in C is a sequence point which means that the expressions separated by the comma are executed from left to right. The value of the whole expression is the value of the rightmost expression, in your case 2.1, which gets assigned to the variable p.

Since the expressions in your example don’t have side effects, the use of the comma separator here makes no sense whatsoever.

The parentheses on the other hand are important since the assignment operator (=) binds stronger than the comma operator (it has higher precedence) and would get evaluated before the comma operator without the parentheses. The result would thus be p == 1.

Konrad Rudolph
In twenty years of writing C code, I have yet to find a really useful non-hackish use for the comma operator.
Graeme Perrow
You can overload the operator in C++ (but not C, obviously) and Boost makes some beautiful use of that to implement list initializations, such as `list_type mylist = 1, 2, 3, 4, 5`.
Konrad Rudolph
@Graeme, i bet you sometimes found `for(i = 0; i < n; i++, j--) ...;` to be useful
Johannes Schaub - litb
@Graeme Perrow -- most of the time I use it is in a for() statement, as in for ( i = 0, j = 0; i < whatever; i++, j++ )... and I end up asking myself "what have I done wrong here" then end up fixing it.
Bob Kaufman
@litb Yes, I have done that, but I always wince a little as I type it. Unless it's temporary throw-away code, I usually try to find a different way of doing it. That usage would fall under the "hackish" category. Maybe it's just me.
Graeme Perrow
Most plausible case for a comma operator in the continuation statement is maybe: `int index = 0; for (node *p = head->next; p != tail; p = p->next, ++index) { something }`
Steve Jessop
Graeme, why would you want non-hackish use if there're plenty of hackish ones?
Michael Krelin - hacker
The comma operator is a hackish operator pretty much by definition -it's to hack in 2 things where only one is allowed.
Michael Burr
Michael, so are compounds ;-)
Michael Krelin - hacker
`if (p = malloc(…), p == NULL)` is nicer to read than `if ((p = malloc()) == NULL)`
Cirno de Bergerac
why not `if( p = malloc( ... ) )`? I usually use `if( ! (p = malloc( ... )) ) return 0;`
Carson Myers
Carson Myers, because there's no comma in it! ;-)
Michael Krelin - hacker
+1  A: 

It's a mistake. the comma operator is similar to ;. It does the one, then the other. so (1,2.1) evaluates to 2.1

p will be 2.1 (or 2, if p is an int and needs to be truncated...)

Brian Postow
It's not necessarily a mistake, just totally useless.
Chris Lutz
uhuh. How often have you seen that functionality and it NOT be a mistake? yes, it couldbe, but HIGHLY unlikely, no?
Brian Postow
I've seen it once: Here, and it's used as a homework exercise to test knowledge about the comma operator. So far, based on all the cases I've seen, use of the comma operator in this way is not a mistake 100% of the time.
Carson Myers
99% of the time I've seen it (in CS 1 projects, etc) is people failing to do 2D arrays... arr[3,5] != arr[3][5]...
Brian Postow
Note, this is not including things like for(i = 0, j = 10; i < j; i++, j--)....
Brian Postow
A: 

all comma seprated expressions will be evaluated from left to right and value of rightmost expression will be returned.

so p will 2.1.

GG
A: 

p = (1, 2.1);

Iam not sure what I know about "p" from the code.

But when I see such code, I'm very sure about the competence of the person who wrote the code :D

codymanix
if you don't know anything about `p` from that code, it makes a suggestion about the competence of the person reading the code :P
Carson Myers
But I know that the person who gave me -1 has no humour. At least not what I call humour x-D
codymanix