precedence of ?: (was: precedence of && (was: precedence of ?:))

Maarten Litmaath maart at cs.vu.nl
Sat Sep 16 02:07:30 AEST 1989


gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
\In article <3260 at solo5.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
\>[...	0 ? 0 : i = 0
\>is accepted, whereas
\>	0 && i = 0
\>is not.]
\>But why allow the `?:' expression, why make it a special case?
\
\I was mistaken in my original response; 0 ? 0 : i = 0 is not legal.

Indeed!  Yet both (SunOS 4.0.1) cc AND gcc 1.35 let it pass due to constant
folding! :-(
Let's see how this will do:

$ cat c.c
main()
{
	int     a, b, c, d;

	a = f();
	b = g();
	d = h();

	test(a ? b : c = d);
}
$ cc -c c.c
"c.c", line 9: illegal lhs of assignment operator
$ gcc c.c
ld: Undefined symbol 
   _test 
   _f 
   _g 
   _h 
$

\However, to save another iteration in this series of postings, note
\that 0 ? i = 0 : 0 is legal.
\	conditional-expression:
\		logical-OR-expression
\		logical-OR-expression ? expression : conditional-expression
\So the interesting question becomes, why is that "expression" for the
\second operand of ?: and not "logical-OR-expression".  (I think that
\"conditional-expression" might result in ambiguity.)  [...]

I don't think so: a ? b ? c : d : e has only one parse.
(If "expression" is OK, "conditional-expression" surely is too!)

Conclusion: cc has a small bug, gcc 1.35 has a more serious one.
-- 
   creat(2) shouldn't have been create(2): |Maarten Litmaath @ VU Amsterdam:
      it shouldn't have existed at all.    |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.lang.c mailing list