Path: utzoo!attcan!uunet!peregrine!ccicpg!cci632!rit!tropix!moscom!ur-valhalla!uhura.cc.rochester.edu!sunybcs!rutgers!tut.cis.ohio-state.edu!ucbvax!amdcad!crackle!tim
From: tim@crackle.amd.com (Tim Olson)
Newsgroups: comp.lang.c
Subject: Re: What's a C expert?
Message-ID: <25999@amdcad.AMD.COM>
Date: 22 Jul 89 17:46:56 GMT
References: <12214@well.UUCP> <6057@microsoft.UUCP> <4722@alvin.mcnc.org>
Sender: news@amdcad.AMD.COM
Reply-To: tim@amd.com (Tim Olson)
Distribution: all
Organization: Advanced Micro Devices, Inc. Sunnyvale CA
Lines: 32
Summary:
Expires:
Sender:
Followup-To:

In article <4722@alvin.mcnc.org> spl@mcnc.org.UUCP (Steve Lamont) writes:
| In article <6057@microsoft.UUCP> paulc@microsoft.UUCP (Paul Canniff 2/1011) writes:
| >In article <12214@well.UUCP> tmh@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@amd.com)