Type conversion within arithmetic expressions. What does ANSI say ?

H Bruce elee24 at castle.ed.ac.uk
Thu Oct 4 21:16:15 AEST 1990


How should the following code fragment compile ?

unsigned char x,y;
unsigned short int z;
z=x*y;

According to the ANSI standard, if the result of x*y exceeds 255, 
will all 16 bits be copied to z  or only the lower 8 ?
(i.e if x=2 and y=200 does z=400 or 400%256 ?)

I have a C compiler for the 8051 which does not copy the upper byte across
even if z=x*y is replaced with z=(unsigned short int)x*y.
I want to confirm that this is a bug.
Microsoft C does convert the result into 16 bits without a type cast.

I can't find reference to this in any "ANSI" C books that we have.


Thanks,

Henry Bruce.



More information about the Comp.lang.c mailing list