c++operator-precedenceconditional-operatorprefix-operator

c++ expression value (operator precedence)


The following expression :-

int main()
{
    int x=2, y=9;
    cout << ( 1 ? ++x, ++y : --x, --y);
}

gives the following output:-

9

As per my understanding, it should return ++y which should be 10. What went wrong?


Solution

  • According to operator precedence,

    1 ? ++x, ++y : --x, --y

    is parsed as

    (1 ? ++x, ++y : --x), --y