Question

I am trying to figure out how the following expression is grouped according to precedence:

int x = 0, y = 0;
someValue ? ++x, ++y : --x, --y;

When someValue is 0 (false) the second part is evaluated, and both x and y become -1. However if someValue is 1 (true) then somehow x becomes 1 but y remains 0, which means the second part of the conditional expression is evaluated too, but only the --y part, which I assume is because of the comma operator, but I can't for the life of me figure out how this is achieved. Any insight would be appreciated.

Was it helpful?

Solution

, has the lowest precedence of all operators, so your expression is parsed as

(someValue ? ++x, ++y : --x), --y;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top