C/C++ Ternary Operator Precedence and Associativity

Although many sources incorrectly place the ternary- and assignment operators on two seperate levels, they in fact share the same precedence level. Note, however, the right-to-left associativity of both, which allows an unparenthesized ternary operation to be assigned to a variable.

If you would like to verify this yourself, take a look at the C++ standard. Alternatively run the following in a compiler of your choice:

int x=0,y=0;

true?x:y=5;

You will find that, after running the above, both x and y are zero, which is consistent with

int x=0,y=0;

true?x:(y=5);

and not with

int x=0,y=0;

(true?x:y)=5;

in which case x=5.

Questions, comments ==> info (at) (this domain)