?: grouping

Morris Keesan keesan at bbncca.ARPA
Fri Jan 13 01:18:48 AEST 1984


-----------------------------
Steve Summit asks,

>>>> 	How should ?: group?  The C Reference Manual says it groups left
>>>>    to right, just like all the binary operators.  In the case of a
>>>>    ternary operator it's not as obvious what "left to right" means,
>>>>    but I would think that a?b:c?d:e should be evaluated as
>>>>    (a?b:c)?d:e (do the leftmost one first).

    I agree that this is the correct interpretation of "left to right" in this
case.  However, what we have here is a genuine bug in the C Reference Manual,
which says in section 7.13 (page 191 of K&R), "Conditional expressions group
right-to-left," and in section 18.1 (page 215 of K&R), "Binary operators and
the conditional operator all group left-to-right. . ."

    I believe that the right-to-left grouping is correct because section 18
(page 214, K&R) says, "This summary of C syntax is intended more for aiding
comprehension than as an exact statement of the language."  This seems to
indicate that section 7.13 takes precedence.

    FLASH!!  I just took a look at the UN*X System V "Programming Guide"
published by Western Electric, and the error has been fixed.  In that manual,
the syntax summary agrees that the conditional operator groups right-to-left.

    Personally, I feel that if practical compilers should issue a
"Warning: cryptic code" message when encountering unparenthesized expressions
like the above.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA



More information about the Comp.lang.c mailing list