[comp.lang.c] `value-preserving' promotions

chris@mimsy.umd.edu (Chris Torek) (08/13/90)

>In article <25836@mimsy.umd.edu> I noted that the ANSI C sign-preserving
type promotion rule can be described as follows:
>>`In ANSI C, when an unsigned type is promoted to some other type, it
>>becomes signed (without changing value) if and only if the new type
>>is actually bigger.'

In article <6T05HF8@xds13.ferranti.com> peter@ficc.ferranti.com
(Peter da Silva) writes:
>What is the rationale for this? There's probably a good reason, but at
>first glance it seems counterintuitive.

I am not the right person to ask for a valid rationale, since I oppose
(note verb tense) this form of promotion.  It does have one positive
aspect, however: it means that if `char' is unsigned, the promotion of
`unsigned char' values to `int' values results in signed ints rather
than unsigned ints in most implementations.  That is,

	foo(i) int i; { ... }
	bar() { char c; ... foo(c); ... }

remains correct unless sizeof(char)==sizeof(int).  (If sizeof(char)==
sizeof(int), the unsigned character `c' should be promoted to an unsigned
int, and thus the type of the argument to function foo() is incorrect and
all bets are off.)

In Classic C, this particular problem was handled by `char' promoting to
`int' always, even in the following cases:

	1. sizeof(char)=1, sizeof(int)=4, CHAR_MIN=0, CHAR_MAX=255
	   (i.e., chars are 1 byte, unsigned); value of char is 255:
	   char promotes to int with value 255
	2. sizeof(char)=1, sizeof(int)=4, CHAR_MIN=-128, CHAR_MAX=127
	   (i.e., chars are 1 byte, signed); value of char is -1:
	   char promotes to int with value -1
	3. sizeof(char)=1, sizeof(int)=1, CHAR_MIN=0, CHAR_MAX=4294967295
	   (i.e., chars are 4 bytes, unsigned); value of char is 4294967295:
	   char promotes to int with value -1.

Cases 1 and 3 appear inconsistent.  (In practise, I believe no implementations
with sizeof(char)==sizeof(int) existed.)

In the new system, under case 3, the char promotes to an unsigned int with
value 4294967295.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris
	(New campus phone system, active sometime soon: +1 301 405 2750)

steve@taumet.com (Stephen Clamage) (08/13/90)

This topic is discussed at some length, including the ruminations of
the ANSI C committee, in P. J. Plauger's column "Standard C" in the
August 1988 (vol 6, no 6) and March 1990 (vol 8, no 3) issues of
"The C Users Journal".

Two incompatible schemes were in wide use on promoting unsigned types,
and after a long battle a consensus was achieved, the losers retiring
bloody but unbowed.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com