Richard Bos wrote:
>
> Eric Sosman <> wrote:
>
> > Mahesh Tomar wrote:
> > >
> > > Please see the code below :-
> > >
> > > void func()
> > > {
> > > unsigned char x,y,z=1;
> > > (z==1) ? (x) : (y) = 1; /* Compiles OK */
> > > ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable
> > > expected" */
> > > }
> >
> > Both lines are erroneous. If either compiles without
> > a diagnostic[*], the compiler is faulty.
>
> What's wrong with the first line? [...]
Unless I'm wrong (it's happened ...), both parse identically:
the `?:' ternary operator "binds more tightly than" the `='
assignment operator. Let's see if I can get there from the
grammar:
- (z==1) is recognized as the "logical-OR-expression"
constituting the first operand of `?:'
- (x) is recognized as the "expression" constituting
the second operand
- (y) is recognized as the "conditional-expression"
constituting the third operand
- ... so the whole business before the `=' is recognized
as a "conditional-expression"
- ... which isn't an lvalue, as required by the `='
operator.
FWIW, the two completely different compilers I tried it
on both issue diagnostics for the line in question, although
gcc accepts *both* lines when run without `-ansi'.
--