Strange multiplication ?

Fridrik Skulason frisk at rhi.hi.is
Fri Jun 15 19:12:27 AEST 1990


I recently noticed that a program I had written behaved in different ways when
compiled with different compilers. The code below illustrates this

            main()
            {
                unsigned char a;
                unsigned long i;
                a = 255;
                i = 256 * a;
                printf("%ld\n",i);
            }

Now - if I compile this on a compiler with 32-bit ints .. no problems. The
value 65280 is printed out.

The problems arise on compilers with 16-bit ints.  Sometimes I get 65280
and sometimes I get -256.  I had thought that when one operand
("a" in this case) was unsigned, the other one (256) and the result would also
be treated as unsigned, so the result of "256*a" should indeed always be
65280.

However, this is not always the case.  Why not ?  What am I missing ?

-frisk

-- 
Fridrik Skulason      University of Iceland  |       
Technical Editor of the Virus Bulletin (UK)  |  Reserved for future expansion
E-Mail: frisk at rhi.hi.is    Fax: 354-1-28801  |   



More information about the Comp.lang.c mailing list