31
int a = 1, b = 0;

if(a, b)
   printf("success\n");
else
   printf("fail\n");

if(b, a)
   printf("success\n");
else
   printf("fail");

This is a cpp file and I got the output in Visual Studio 2010 as

fail
success

Why this behavior? Could you please explain?

indira
  • 6,569
  • 18
  • 65
  • 80
  • 9
    You know that it's using the comma operator in the condition, yet you have to ask? – Daniel Fischer May 10 '13 at 04:09
  • You can just add a breakpoint on first statement and execute program line by line using F10 to see the flow yourself. – vish213 May 10 '13 at 04:13
  • @DanielFischer I know that comma acts as a sequence point in statements but not in arguments to functions.Can you tell me whether it is a sequence point inside `if`?I intuitively feel **yes**,but I would like you to confirm. – Rüppell's Vulture May 10 '13 at 04:36
  • @Rüppell'sVulture The `if` takes an expression, so in `if (a, b)` it is the comma operator, hence a sequence point. In function argument lists or initialiser lists, a comma is just a separator, not the comma operator [well, we could call `printf("%d\n", (1,2));` and then we'd have a comma operator in the second argument of `printf`, but we need parentheses for that]. – Daniel Fischer May 10 '13 at 04:42
  • @DanielFischer Can you look into this question of mine please.I didn't find the answer I needed. http://stackoverflow.com/questions/16475918/to-copy-files-in-binary-mode-why-it-doesnt-work-when-we-read-to-and-write-from/16476100?noredirect=1#16476189 – Rüppell's Vulture May 10 '13 at 06:15

2 Answers2

71

http://en.wikipedia.org/wiki/Comma_operator:

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).

In your first if:

if (a, b)

a is evaluated first and discarded, b is evaluated second and returned as 0. So this condition is false.

In your second if:

if (b, a)

b is evaluated first and discarded, a is evaluated second and returned as 1. So this condition is true.

If there are more than two operands, the last expression will be returned.

If you want both conditions to be true, you should use the && operator:

if (a && b)
Yang
  • 7,712
  • 9
  • 48
  • 65
  • 1
    So what is the point of doing such conditions? could someone provide a better and useful example – Pars Sep 14 '16 at 06:08
  • 7
    @Pars It's useful if you want to perform an operation that causes side effects, but don't need the actual return value. For example, I had this condition in one of my projects: `if (numeric_read(str, &err), !err)`. Had I used `&& !err` instead of `, !err`, I wouldn't be able to read numbers that were 0. – Ryan Pendleton Oct 21 '16 at 21:05
  • 1
    so what's the difference between `,` and `||`? – FalcoGer Feb 05 '20 at 21:43
  • 1
    @FalcoGer `,` returns the value of the last expression. `||` is boolean OR returning true if either operand is true. – Yang Feb 06 '20 at 04:21
3

Here is an example, provided by wikipedia, which shows another use case:

The comma can be used within a condition (of an if, while, do while, or for) to allow auxiliary computations, particularly calling a function and using the result, with block scoping:

if (y = f(x), y > x) { ... // statements involving x and y }

// See this Wikipedia discussion

Many C programmers have encountered the comma in the initializer part of a for statement, but not as many have seen it used in an if statement. In the case above it allows you to initialize y before the if statement tests the condition y>x.

jmkuss
  • 136
  • 1
  • 2