C quiz questions/answers?

Kevin Buettner plug-devel@lists.PLUG.phoenix.az.us
Mon Nov 5 18:11:32 2001


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