Int and Char

Chris Torek chris at umcp-cs.UUCP
Sat Jul 26 10:34:55 AEST 1986


In article <3250 at jhunix.UUCP> ecf_eprm at jhunix.UUCP (Paul R Markowitz) writes:
>Chars are NOT SIGNED, there is no char called -1.

Whether characters are signed or not is compiler-dependent.  X3J11
says so; and even K&R says so (p. 40).

>If you want a variable that takes on negative values, use a short.

Or, optionally, a typedef and/or a sign-extension macro:

	/*
	 * types.h: system dependent types.
	 *
	 * Turn on the appropriate things for your system.
	 */

	/* compiler error until installer adjusts this */
	THIS FILE WAS NOT SET UP FOR YOUR SYSTEM YET!

	/* a small integer, signed */
	/* typedef char smallint; */
	/* typedef short smallint; */

	/* take a number, treat as eight bits, and sign extend */
	/* #define Sign8(c) ((int)(char)(c)) */

	/* use this one on Suns to avoid a compiler bug with constants */
	/* this also works on 32 bit machines with sign extending `>>' */
	/* #define Sign8(c) (((c) << 24) >> 24) */

	/* #define Sign8(c) ((c) & 0x80 ? (c) - 0x100 : (c)) */

Finally, for X3J11 C:

	/* typedef signed char smallint; */

	/* #define Sign8(c) ((int)(signed char)(c)) */
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
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