fscanf bug in TC (BC) C++ 2.0

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Fri Apr 19 13:35:00 AEST 1991


In article <41392 at cup.portal.com>, ts at cup.portal.com (Tim W Smith) writes:
> Suppose one of the types is single precision floating point.
> 	long data;
> 
> 	data = nextbyte() << 24;
> 	data |= nextbyte() << 16;
> 	data |= nextbyte() << 8;
> 	data |= nextbyte();

> 	printf( "%f", data );

> Oops!  No floating point at compile time but needed at runtime.
> ps: of course, I would never do this!

I'm so glad to hear that, as it doesn't do what you intended.
The %f format expects to find a DOUBLE value on the stack, which in TC
is 64 bits, but 'long' is only 32 bits.  [See the .signature.]  To make
this example work, it would need to be
	{ union {long L; float F;} pun;		/* we can't use a cast */
	  pun.L = data;				/* because we DON't want */
	  printf("%f", pun.F);			/* conversion */
	}
This, of course, "declares a floating-point variable", so the floating-
point routines should be linked in.
-- 
Bad things happen periodically, and they're going to happen to somebody.
Why not you?					-- John Allen Paulos.



More information about the Comp.lang.c mailing list