Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!uwvax!dogie.macc.wisc.edu!csd4.milw.wisc.edu!lll-winken!uunet!seismo!esosun!cogen!celerity!celit!billd From: billd@celerity.UUCP (Bill Davidson) Newsgroups: comp.lang.c Subject: Re: What's a C expert? Message-ID: <343@celit.UUCP> Date: 20 Jun 89 04:45:33 GMT References: <12214@well.UUCP> <6057@microsoft.UUCP> Sender: news@celerity Reply-To: billd@celerity (Bill Davidson) Distribution: all Organization: FPS Computing Inc., San Diego CA Lines: 34 In article <6057@microsoft.UUCP> paulc@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