What's a C expert?

Steve Lamont spl at mcnc.org
Sun Jul 23 00:03:26 AEST 1989


In article <6057 at microsoft.UUCP> paulc at microsoft.UUCP (Paul Canniff 2/1011) writes:
>In article <12214 at well.UUCP> tmh at well.UUCP (Todd M. Hoff) writes:
>>           What do you need to know to be an expert C programmer?
> [other "qualifications" deleted]
>                          ... can tell why the following code
>prints "false" (on 8-bit char systems).
>
>	char x = 0xff;
>
>	if (x != 0xff)
>		printf("FALSE\n");

The answer I assume you are looking for is that in the if-statement, the
value of x is expanded to an int and, since it is declared char rather
than unsigned char, the sign bit (high order) bit is propagated, making
x == -1 decimal.  This clearly differs from 0xff, which is, of course 255,
decimal.

However, when I tried this on my IRIS 4D to confirm my theory, I got the
"wrong" answer.  In other words, it did not print FALSE when run.  My
assumption is that the C compiler is not propagating the sign when
promoting x to an int in the if-statment.  (I don't read MIPS assembly
code well enough to verify this)

Is the Silly Graphics compiler broken, or is the behavior in the above
code simply unpredictable?  C "experts," what say you?

				(not an "expert", I just found the hat)
-- 
							spl
Steve Lamont, sciViGuy			EMail:	spl at ncsc.org
North Carolina Supercomputing Center	Phone: (919) 248-1120
Box 12732/RTP, NC 27709



More information about the Comp.lang.c mailing list