5
int main(void)
{
  int a=0, b=20;
  char x=1, y=10;
  if(a,b,x,y)
    printf("bye");
  return 0;
}

How does "if" condition in the above code work work? Would the value of "y" be only considered by "if"?

Jens
  • 69,818
  • 15
  • 125
  • 179
sreejith.virgo
  • 333
  • 1
  • 4
  • 15

4 Answers4

17

Yes, the value of the comma operator is the right operand. Because none of the other operands have side effects, this boils down to if (y).

Joey
  • 344,408
  • 85
  • 689
  • 683
  • does this mean , if a,b,c would be 'false', even then it'll directly go to y only ? – Deepanshu Goyal Aug 26 '13 at 08:45
  • 4
    @Deepanshu No, [the comma operator](http://en.wikipedia.org/wiki/Comma_operator) doesn't care about the value of any operands except the last one. They're all evaluated and the result thrown away, there is no [short-circuit evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation) happening. – unwind Aug 26 '13 at 08:47
  • Probably. Use `&&` or `||` along with `(` and `)` if you want to use some of them or all of them. – haneefmubarak Aug 26 '13 at 08:48
  • oh nice....thnx @unwind – Deepanshu Goyal Aug 26 '13 at 08:48
  • @Deepanshu If you let me add a precision to the comment of unwind, I would say that the most important part of his comment is "They're all evaluated". In case of functions calls, all calls are made, and then only the result of the last call will be used for the test (if suitable, i.e. not a function returning a struct). – Bentoy13 Aug 26 '13 at 10:07
  • Admittedly, I left out the evaluation part because it makes no difference here and I didn't want to complicate the answer. – Joey Aug 26 '13 at 10:09
6

From Wikipedia:

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

This in effect means that only the final operand is evaluated for truthfulness, the results of the previous operands are discarded.

In if(a,b,x,y) only the truthfulness of y is considered and therefore whatever y has evaluated to will be considered as true/false.

In your case y is equal to 10 which is considered true in C, therefore the if check will also evaluate to true and the if block will be entered.

You might want to consider this very popular question on StackOverflow for its uses (and misuses).

Community
  • 1
  • 1
Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • 2
    I suppose it's only fair that a question that should rightly be closed for lack of research gets an answer from Wikipedia. – Jim Balter Aug 26 '13 at 09:23
4

,(comma) operator separates the expression.if the multiple values are enclosed in round bracket then the last value in round bracket gets assigned to variable.

e.g a=(x,y,z);
then  a=z;

while if,

a=x,y,z;

then above expression gets evaluated to (a=x);

Please refer this.

Rohan
  • 3,068
  • 1
  • 20
  • 26
  • 1
    comma operator has the least precedence. – ajay Aug 26 '13 at 08:51
  • 2
    @YuHao Of course that will compile ... it defines a, y, and z, initializing a. What won't compile is `int (a=x),y,z;`, making this answer wrong. To R.S.: comma is only an operator in expressions. `(x,y,z)` is an initializer expression, so the comma is an operator there. But declarations aren't expressions, so `int (a=x),y,z;` is nonsense and that part of your answer should be reworded. – Jim Balter Aug 26 '13 at 09:25
0

As Joey said above this evaluates to nothing more than

if (y)
    ....

It is important to observe that if your code had read:

int main(void)
{
  int a=0, b=20;
  char x=1, y=10;
  if(a++,b++,x++,y)
    printf("%d, %d, %d, %d\n", a, b, (int)c, (int)y);
  return 0;
}

The output would have been

1, 21, 2, 10

All the increments would have been executed but for the purposes of evaluating the condition

(a++,b++,x++,y)

the only one that matters is the last one, namely y

Community
  • 1
  • 1
amrith
  • 953
  • 6
  • 17