comp.lang.c
[Top] [All Lists]

Re: ?: as an lvalue

Subject: Re: ?: as an lvalue
From: Eric Sosman
Date: Mon, 31 Mar 2008 14:43:52 -0400
Newsgroups: comp.lang.c


Dave Hansen wrote:
On Mar 30, 7:19 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Rahul said:

Hi Everyone,
 I have the following piece of code, and i expected an error, however
i don't get an error,
int main()
{
int aa=0,b=0;
1>0?aa:b = 10;
This is a syntax error (which therefore requires the implementation to
diagnose it as such). The syntax of the conditional operator is:


I believe the syntax is legal.  It just doesn't do what the OP wanted
it to.

Consider that it parses as

   (1>0)?(aa):(b=10);

    Would you care to place a small wager on that?

    You could refer to the formal grammar in the Standard
to settle the question, or you could ask informally which
of = and ?: "binds more tightly."  The parse you suggest
would follow if = binds more tightly, in which case

        x = a ? b : c;

would parse as

        (x = a) ? b : c;

Since we know that it actually parses as

        x = (a ? b : c);

you may be about to lose some money ...

--
Eric.Sosman@xxxxxxx


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy