🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9667

Re: Signed char - What Foolishness Is This!

#9667
From: Chris Torek
Date: Tue, 28 Oct 1986 22:35
62 lines
3372 bytes
>In article <14@ems.UUCP> mark@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@zen.BERKELEY.EDU> chapman@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@umcp-cs		ARPA:	chris@mimsy.umd.edu
Technical Details
Message-ID: <4037@umcp-cs.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!umcp-cs!chris
References: <8719@duke.duke.UUCP> <8273@sun.uucp> <14@ems.UUCP> <661@zen.BERKELEY.EDU>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!umcp-cs!chris
From: chris@umcp-cs.UUCP (Chris Torek)
Newsgroups: net.lang.c,net.micro.pc
Subject: Re: Signed char - What Foolishness Is This!
Message-ID: <4037@umcp-cs.UUCP>
Date: Tue, 28-Oct-86 17:35:29 EST
Article-I.D.: umcp-cs.4037
Posted: Tue Oct 28 17:35:29 1986
Date-Received: Tue, 28-Oct-86 23:26:24 EST
References: <8719@duke.duke.UUCP> <8273@sun.uucp> <14@ems.UUCP> <661@zen.BERKELEY.EDU>
Reply-To: chris@umcp-cs.UUCP (Chris Torek)
Organization: University of Maryland, Dept. of Computer Sci.
Lines: 62