Unsigned character conversion to (unsigned)int.

Karl Heuer karl at haddock.ima.isc.com
Mon Jun 11 06:28:34 AEST 1990


In article <866 at sce.carleton.ca> graham at sce.carleton.ca (Doug Graham) writes:
>	unsigned char c = 0x80;
>	unsigned long l = c << 8;
>On a machine with 16 bit integers, and 32 bit longs...

The correct (ANSI) behavior is to produce a (presumably unintended) result of
0xffff8000.

The polite behavior is to also produce a warning that a questionably signed
value has been used in a context where the signedness is significant.

The appropriate fix is to use an explicit cast, so that the code will produce
0x8000 on either the value-preserving model or the unsigned-preserving model.

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list