BUG - unsigned long cast to double
mcdonald at uxe.cso.uiuc.edu
mcdonald at uxe.cso.uiuc.edu
Sat Aug 13 22:11:00 AEST 1988
> printf ("y: %d -> %lf z: %u -> %lf\n", y, (double) y, z, (double) z);
> y++; z++;
> printf ("y: %d -> %lf z: %u -> %lf\n", y, (double) y, z, (double) z);
I tries this on my IBM-PC with microsoft C 5.1 and of course got
garbage. The reason is obvious: %d SHOULD be %ld and %u should be %lu.
With these changes, the output, all 4 numbers, are exactly what one
would expect for a two's complement integer machine. The problem
with the original is that printf picks up the wrong bytes to output,
due to the size of the integer arguments not agreeing with the
format specifications. Try a corrected program and see what happens.
Doug McDonald
More information about the Comp.lang.c
mailing list