Unsigned character conversion to (unsigned)int.

Doug Graham graham at sce.carleton.ca
Wed Jun 6 09:49:34 AEST 1990


I encountered a program which does something like:

	unsigned char c = 0x80;
	unsigned long l = c << 8;

On a machine with 16 bit integers, and 32 bit longs, what should the value
of "l" be in ANSI C? Microsoft, and Zortech C thinks it is 0x8000 which makes
sense to me, and Turbo C thinks it is 0xffff8000, which is what K&R2 would
seem to indicate is the correct answer.

When "c" is converted to integer before being used in the expression
"c << 8", is it converted to unsigned or signed integer? I think K&R2 (A6.1)
says signed, because all possible values of an unsigned char can be
represented in a signed integer. Thus the signed integer 0x80 would
be shifted left to give the signed integer 0x8000, which is then
sign extended to a signed long 0xffff8000, before being assigned to "l".

Doug.



More information about the Comp.lang.c mailing list