# Meaning of this statement?

• 07-14-2006
@nthony
Meaning of this statement?
I just compiled successfully what I thought was an invalid statement... now I'm left wondering what the heck it means since I've never seen such a statement in C before. It would appear to be some sort of equality statement... Here it is:
Code:

`if((handle1, "Some String", "Title", int1 | int2 | int3)== int4)`
I would guess it takes on the form "(term1, term2, term3, ...)" but what does it mean (or evaluate to) when used in an equality expression?

p.s. if this compares each value seperated by commas with the int4 value... then I'm wondering why I haven't learned about this shortcut earlier!!!

(if this is some super-common statement in C that I just somehow didn't come across before, then I apologize for being a newbie :D )
• 07-14-2006
SlyMaelstrom
I don't see how that would do anything. The comma operator is basically just used to create an evaluation list in a statement. For instance in a for statement, you could do:
Code:

`for(i = 0, j = 10; i < j; ++i, --j)`
This would do as you expected. Assign 0 to i and 10 to j and loop incrementing i and decrementing j until i => j.

The comma operator, I believe returns the result. Which, unless you put it somewhere, gets discarded. In the case of your statement above, it should act no differently than
Code:

`if(int1 | int2 | int3 == int4)`
You can test it yourself
Code:

```    if((1,2) == 3)       std::cout << "if((1,2) == 3) is True!";     if((1,2,3) == 3)       std::cout << "if((1,2,3) == 3) is True!";     if((1,2,3,4) == 3)       std::cout << "if((1,2,3,4) == 3) is True!";```
I could be wrong, though. There might be something else going on there that I'm not seeing. I don't think so, though.
• 07-14-2006
OnionKnight
The program goes through the whole list and returns the last element and since all the statements were useless it's the same as
Code:

`if (int1 | int2 | int3 == int4)`
• 07-14-2006
@nthony
Indeed you are right! Only the last value is actually used in comparison.
I knew about the comma's use in the for statement, but I had no idea that it could be used in a similar capacity in all equality statements.... and to think I almost thought it might be a shorthand for comparing many values against a single value... too good to be true :P

Thanks for clearing that up for me!
• 07-14-2006
quzah
Quote:

Originally Posted by SlyMaelstrom
The comma operator, I believe returns the result. Which, unless you put it somewhere, gets discarded. In the case of your statement above, it should act no differently than
Code:

`if(int3 == int4)`

You seem to have overlooked something...

Quazh.
• 07-14-2006
SlyMaelstrom
Ah, yes, I got so caught up in his question about the comma that I did infact overlook the bitwise operator. I shall edit my post.
• 07-16-2006
sughandh
The comma operator,bit wise OR operator(|) have left to right associativity meaning that

if

a=r(x,y,...,z)
is equivalent to
a=z; after all the statement x,y,... are executed

http://h30097.www3.hp.com/docs/base_...E/DOCU_059.HTM
• 07-16-2006
quzah
I'm not exactly sure what your point is here. The oring takes place regardless of the comma operator. Thus, you should be saying:
Code:

`a = w, x, y | z;`
Is the same as:
Code:

`a = y | z;`
Ignore the warnings, since we're talking about crappy code originally, as this is a further illustration of said crappy code:
Code:

```#include<stdio.h> int main( void ) {     int a = (1, 2, 3);     int b = (0, 1 | 2 | 4);     int c = (1 | 2 | 4, 0);     printf( "a is %d, b is %d, c is %d\n", a, b, c );     if( (a,c,b) == (1 | 2 | 4) )         printf( "b is 7\n" );         return 0; } /* a is 3, b is 7, c is 0 b is 7 */```
Quzah.