Possible C compiler bug on 8086 machines

Edward John Kalenda ekalenda at cup.portal.com
Sat Jan 12 19:52:17 AEST 1991


> The following program demonstrates a printf that doesn't seem to work
> properly when compiled using a variety of C compilers for an 8086 machine.
> The program is as follows:
> 
> #include <stdio.h>
> main()
> {
>    int x,y;
>    x = 65536/512;
>    y = 512;
>    printf("This works : %d, %d\n",x,y);
>    printf("This doesn't work : %d, %d\n",65536/512,512);
> }
> And here is a sample output: 
> This works : 128, 512
> This doesn't work : 128, 0
> 
> Does anyone have any idea why?
> Is this a problem with my machine or the printf routine?

The problem is that 65536/512 results in a LONG being pushed on the stack
so the send %d gets the high word of the long, which is zero. The reason
it works in the first case is that the long result is converted to an int
when assigned to the int x. I use MSC 6.0 with /X4 to catch oddities like
this at compile time. Most UNIX programmers run into this when moving from
the 32 bit machines to DOS.

Ed
ekalenda at cup.portal.com



More information about the Comp.lang.c mailing list