Turbo C bug !

Norman Diamond diamond at csl.sony.JUNET
Tue Jan 24 10:41:38 AEST 1989


In article <PK-TLE.89Jan21103247 at shai.sics.se>, pk-tle at sics.se (Tommy Levitte) writes:

> #define X 240
> #define Y 240
> #define Z ((X)*(Y))
> 
> void fn(unsigned long i)
> {
>  printf("%ld %ul\n",i,i)
> }
> 
> main()
> {
>  fn(Z);
> }

> Running this gave me a nice surprise:
> -7936 2147475712

Looks like they FIXED a bug in the earlier version.  240 is an int,
240 * 240 is the int -7936, convert it to unsigned long for i and get
2147475712.  Tell printf that it's signed long and it changes back to
-7936; tell printf that it's unsigned long and you get 2147475712.

> But if I change fn(Z) with fn(57600), it gives me the right numbers:
> 57600 57600

57600 is a long.  Convert it to unsigned long for i and get 57600.
The rest is obvious.

> Fix : Put an L at the end of 240 !!!!

Yes, that is one way to fix your own bug.

[Now to fix the bug in inews that requires more of my text than yours]
[again]
-- 
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp at relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?



More information about the Comp.lang.c mailing list