Signed char - What Foolishness Is This!

Mark Colburn mark at ems.UUCP
Mon Oct 20 13:34:40 AEST 1986


In article <8273 at sun.uucp>, guy at sun.UUCP writes:
> > I can understand the desirability of allowing 'signed char' for gonzo
> > programmers who won't use 'short',
> 
> It's not a question of "gonzo programmers who won't use 'short'.

It is important to note that K&R define that:

	char  8 or more bits
	short 16 or more bits

Although these values may be implementation specific.  On my 68020 based
machine, shorts are 16 bits.  When I need an 8 bit unsigned value (e.g. a byte)
in my code (which happens quite frequently when you are writing software to
support 8 bit CPU's) I use 'unsigned char'.

I got myself into all sorts of trouble when I was first using C because I
assumed that if an int is 16 bits, then a short must be 8.  Right?  Wrong!
On the compiler that I was using, int was 16 bits and so was short.  This
is consistent with K&R (and, I believe, the proposed ANSI standard).

Therefore, the only portable way to express a true byte (8-bit) value is with
an 'unsigned int' declaration.  This may still get you into trouble when you
are working on a compiler that uses characters that are more than 8 bits.
Don't laugh, there are some out there.  It is also allowed for in the language
definition.  Notice that a character may be 8 or more bits.  Since machines
that use chars that are larger than 8 bits are relatively infrequent, I
callously disregard their existence in my code.  (I am sure that I will get
bitten by it one of these days, but hey, gives a guy some kinda job security).

-- 
Mark H. Colburn             UUCP: ihnp4!rosevax!ems!mark
EMS/McGraw-Hill              ATT: (612) 829-8200
9855 West 78th Street
Eden Prairie, MN  55344



More information about the Comp.lang.c mailing list