precedence of ?:

Michael Henning michi at anvil.oz
Fri Sep 15 08:41:39 AEST 1989


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 '='. Compilers which
accept '0 ? 0 : i = 0' as correct are simply wrong. I strongly recommend
the book

	"C A Reference Manual" by Harbison and Steele (Prentice Hall)

for the answers to problems such as the one above. It is the best reference
text for C I have seen so far.

					Michi.
-- 
| Michael Henning            |  Internet   : michi at anvil.oz{.au}              |
| Anvil Designs              |  JANET      : michi%anvil.oz at uk.ac.ukc         |
| P.O. Box 954               |  ARPA,Bitnet: michi%anvil.oz.au at uunet.uu.net   |
| Toowong 4066, Australia    |  UUCP       : ...!uunet!munnari!anvil.oz!michi |



More information about the Comp.lang.c mailing list