Implicit and explicit casts...

T. William Wells bill at twwells.uucp
Thu Oct 20 10:38:45 AEST 1988


In article <14028 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
:       short s;
:       int i;
:       i = (unsigned char)s;
:
: which moves the value in `s' through four(!) types before reaching
: `int' and storing in `i':
:
:       what                            type
:       ----                            ----
:       s                               short, lvalue
:       <sign extended value>s          int, rvalue
:       <truncated value>s              unsigned char, rvalue
:       <expanded truncated value>s     unsigned int, rvalue

I understand that there are some compilers that do not do the
truncation properly. In other words,

	int     i = (unsigned char)0xFFFF;

stores 0xFFFF in i, instead of 0xFF (assuming 8 bit bytes, of course).

Such compilers are broken, but if portability is a concern, one
should avoid assuming that casting (or for that matter, assigning to
a char) properly truncates a value.

---
Bill
novavax!proxftl!twwells!bill



More information about the Comp.lang.c mailing list