What's a C expert?

Tim Olson tim at crackle.amd.com
Sun Jun 18 03:46:56 AEST 1989


In article <4722 at alvin.mcnc.org> spl at mcnc.org.UUCP (Steve Lamont) writes:
| 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");

You also need to know why the statement above is not necessarily true ;-)

| 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.

Chars are not necessarily signed by default.  Many implementations make
chars unsigned.  This is the reason why the "signed" keyword was added
to ANSI C.

Having the sign of chars be undefined allows the implementation to be as
efficient as possible with respect to converting between chars and ints.


	-- Tim Olson
	Advanced Micro Devices
	(tim at amd.com)



More information about the Comp.lang.c mailing list