Precedence and associativity are different concepts, but technically the C and C++ standard specifies none. Instead they give the grammar rules to deduce the structure of the expression.
The relevant rules are:
conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
expression:
assignment-expression
expression , assignment-expression
primary-expression:
( expression )
postfix-expression:
primary-expression
...
And so on...
The idea is that each type of expression can generate a composite expresion or another type of expression of lower precedence. You can only go up to the root expression
by using parenthesis.
With that in mind, note that the conditional-expression
that uses the ?:
actually has different types of expressions in each of the three subexpressions. The middle one is expression
so it will accept any kind of expression, even with ,
or =
(no ambiguity here because of the ending :
).
But note that the last one is assignment-expression
, that is any kind of expression except the one with ,
. If you want to use that, you will have to enclose it with ()
creating a primary-expression
instead.
Bonus explanation: the first expression is logical-or-expression
, and if you look carefully to the grammar you'll see that it excludes assignment operators, the conditional operator and the comma operator.
So your expression:
b = a > 0 ? ++b, ++a : --a, b = 0
Is actually a expression
comma assignment-expression
, where the first expression
is b = a > 0 ? ++b, ++a : --a
and the second assignment-expression
is b = 0
.
And so on...