Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the precedence of comma operator inside conditional operator in C++?

What's happening here?

#include <iostream>
using namespace std;

int main(){

    int x=0,y=0;
    true? ++x, ++y : --x, --y; 
    cout << "x: " << x << endl;
    cout << "y: " << y << endl; //why does y=0 here?

    x=0,y=0;
    false ? ++x, ++y : --x, --y; 
    cout << "x: " << x << endl;
    cout << "y: " << y << endl;
}

x: 1
y: 0

x: -1
y: -1

The second case seems fine. I would expect both x and y to increment to 1 in the first case but only the left hand operand increments.

like image 331
hhbilly Avatar asked Aug 27 '12 04:08

hhbilly


People also ask

What will be the correct order of conditional operator in C?

The conditional operator of the C programming language works as follows: The condition is evaluated first and the result of the condition is implicitly converted to bool. If the condition evaluates to be true the first statement -- the statement after the question mark will get executed.

Which operator has the lowest precedence assignment or comma?

The comma operator has the lowest precedence.

What are the uses of comma and conditional operator?

The comma operator (,) allows you to evaluate multiple expressions wherever a single expression is allowed. The comma operator evaluates the left operand, then the right operand, and then returns the result of the right operand.

What is the comma operator in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.


2 Answers

The first one is equivalent to:

(true  ? (++x, ++y) : (--x)), --y; 

The second one is equivalent to:

(false ? (++x, ++y) : (--x)), --y; 

Thus the --y is always executed. In the first line, the increments are executed first so x = 1, y = 0 is expected. In the second line, the decrement of x is executed first so x = -1, y = -1 is expected.


As noted in a comment (to another answer) by Barmar:

And in case anyone is wondering why the comma between ++x and ++y doesn't have the same effect, it's because (true? ++x) would not be valid at all. So the compiler keeps scanning until it finds the :, but beyond that it stops when it reaches a lower precedence operator [(, in this example) or the end of statement].

like image 178
Jonathan Leffler Avatar answered Sep 21 '22 21:09

Jonathan Leffler


The y is zero because comma has the lowest precedence among all C++ operators. Because its precedence is lower than that of the ternary conditional operator, the conditional operators are parsed as true? ++x, ++y : --x and false? ++x, ++y : --x. In both cases, the --y statement is executed unconditionally.

EDIT The first comma is different because the compiler has found a ?, so now it needs a : to complete the "when true" expression of the conditional. That is why both ++x and ++y are taken in.

like image 28
Sergey Kalinichenko Avatar answered Sep 17 '22 21:09

Sergey Kalinichenko