C quiz questions/answers?

Julian M Catchen plug-devel@lists.PLUG.phoenix.az.us
Mon Nov 5 18:28:04 2001


Well, I had to add parentheses on the win32 machine I was using at work, 
but thought that it should still work under gcc.  I guess it doesn't...


On 2001.11.05 18:02 Kevin Buettner wrote:
> On Nov 5,  6:53pm, Julian Catchen wrote:
> 
> > The answer is B.  Although some compilers whine without parentheses
> around
> > the "i < 100" line.
> [...]
> > ps - I checked my answer after I came up with a solution.... :)
> 
> You did??
> 
> Which compiler did you use?  gcc says:
> 
>     [kev@mesquite ctests]$ gcc -o foobar -g foobar.c
>     foobar.c: In function `main':
>     foobar.c:8: invalid lvalue in assignment
> 
> The problem with line 8 is that
> 
>     i < 100 ? x = i : x = 100;
> 
> is actually interpreted by the compiler as:
> 
>     (i < 100 ? x = i : x) = 100;
> 
> In order to make this work as desired, you'd have to write it as:
> 
>     i < 100 ? x = i : (x = 100);
> 
> Or even better:
> 
>     x = i < 100 ? i : 100;
> 
> Kevin
> _______________________________________________
> PLUG-devel mailing list  -  PLUG-devel@lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
> 
> 
-- 
mail : julian @ catchen.org         | ( topeka )
www  : http://catchen.org/topeka/   |  phx, az
sent : Mon Nov 05, 2001 06:12PM MST | 
  Whenever there is a conflict between human rights
  and property rights, human rights must prevail.
                                  --Abraham Lincoln