Signed char - What Foolishness Is This!

Chris Torek chris at umcp-cs.UUCP
Wed Oct 29 08:35:29 AEST 1986


>In article <14 at ems.UUCP> mark at ems.UUCP (Mark Colburn) writes:
>>It is important to note that K&R define that:
>>	char  8 or more bits
>>	short 16 or more bits

In article <661 at zen.BERKELEY.EDU> chapman at cory.Berkeley.EDU.UUCP
(Brent Chapman) writes:
>Just _WHERE_ does K&R say this?

K&R do not define minimum sizes.  They do provide a table listing
some existing implementations (pp. 34 and 182), and they say also
this:

  A character or a short integer may be used wherever an integer
  may be used.  In all cases the value is converted to an integer.
  Conversion of a shorter integer to a longer always involves sign
  extension; integers are signed quantities.  Whether or not
  sign-extension occurs for characters is machine dependent, but
  it is guaranteed that a member of the standard charcter set is
  non-negative.  Of the machines treated by this manual, only the
  PDP-11 sign-extends.

The most recent X3J11 (`ANSI C') draft I saw, however, *did* define
minimum sizes in bits for `char', `short', `int', and `long'.  It
still left `char' sign extension up to the compiler, but added the
types `signed char' and `unsigned char'.

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

Note that `unsigned char' is not valid according to K&R (although
most C compilers have such a type).

In a particular set of `portable' programs I wrote (and am still
writing), I needed 8, 16, 24, and 32 bit integers, with both
signed and unsigned varieties for 8, 16, and 24 bits.  Toward this
end I have one machine-dependent `#include' file called `types.h';
in it I define the following:

	UnSign8(n)		produce an unsigned 8 bit integer value
				given the possibly-signed integer value n
	Sign8(n)		produce a sign extended 8 bit integer
				value (i.e., 128 -> -128; 255 -> -1)
	UnSign16(n)		produce an unsigned 16 bit value
	Sign16(n)		sign extend a 16 bit value
	UnSign24(n)		produce an unsigned 24 bit value
	Sign24(n)		sign extend a 24 bit value

	i32			a 32 (minimum) bit integer type

Instead of trying to find types of the proper sizes, I have one
that is large enough for all, and a set of macros to coerce it so
as to properly represent the smaller values.  I believe this can
be implemented on any machine on which the software could ever run.
The macros themselves are machine dependent, but well-isolated.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list