precedence of ?:

T. William Wells bill at twwells.com
Sat Sep 30 15:07:47 AEST 1989


In article <658 at anvil.oz> michi at anvil.oz (Michael Henning) writes:
: In article <1265 at gmdzi.UUCP>, wittig at gmdzi.UUCP (Georg Wittig) writes:
: > How should
: >     0 ? 0 : i = 0
: > be interpreted?
: >
: > 1)  as      (0) ? (0) : (i=0)
: >     resulting in a (strange but) legal expression
: >
: > or 2)       as      (0 ? 0 : i) = 0
: >     resulting in a syntax error
: > ?
:
: The correct interpretation is
:
:       (0 ? 0 : i) = 0
:
: because the precedence of ':' is higher than that of '='.

No it isn't. Because the LHS of an assignment operator is a unary
expression, not a conditional expression. Precedence rules are not
adequate for analyzing C expressions.

It is not the case (as I'm assuming that Mr. Henning is asserting)
that the expression is parsed "(0 ? 0 : i) = 0" and then rejected
because "0 ? 0 : i" is not an lvalue. Instead, there is no valid
parse of the expression and the lvalueness of "0 ? 0 : i" is not
relevant.

Didn't we just finish beating this into the ground?

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com



More information about the Comp.lang.c mailing list