What's a C expert?

Bill Davidson billd at celerity.UUCP
Tue Jun 20 14:45:33 AEST 1989


In article <6057 at microsoft.UUCP> paulc at microsoft.UUCP (Paul Canniff 2/1011) writes:
>[an expert C programmer] can tell why the following code
>prints "false" (on 8-bit char systems).
>
>	char x = 0xff;
>
>	if (x != 0xff)
>		printf("FALSE\n");

On a sign extending system with signed chars, it's because the signed
char x is extended to be -1 and the "if" uses an int comparison with the
int constant 0xff == 255.

This does not have to work on all machines or compilers.  Section 2.7
of K&R first edition (and section 6.1 of the C Reference Manual) allows
chars to be either signed or unsigned and sign extension of chars is
left as "hardware dependant".  Shorts and int's must be sign extended.
Only char's are left to the implementor.  I tried this on 3 completely
different kinds BSD machines with (I believe) pcc based compilers and
it only printed "FALSE" on one of them.  Section 2.7 of K&R second
edition is the same on this.  I can't seem to find the same stuff in the
reference manual for the second edition.  Section 6 changed quite a
bit in organization.

The point: the above code is implementation dependant and it is
not portable to depend on having it work a certain way.

P.S.  It took me about 3 seconds (I read little slow :-) to find the
right section in my tattered copy K&R1 and only had to look it up because
I don't have the section numbers memorized and I wanted to refrence them
so that the non-experts :-) out there can look it up.  Does that qualify
me as an expert :-).

Bill Davidson		...!{ucsd,sdsu,fpssun,cogen,chip,photon}!celerity!billd



More information about the Comp.lang.c mailing list