fscanf bug in TC (BC) C++ 2.0

Tim W Smith ts at cup.portal.com
Thu Apr 18 17:52:34 AEST 1991


< that the algorithm is imperfect: if the program isn't using
< floating point, %e, %f, and %g can't be needed, but they might
< not be needed if the program is using floating point, either.
< However, "program uses floating point" is in principle computable
< at compile time, while "%e, %f, or %g might get passed to printf"
< isn't.)

Suppose you have a program that is receiving data a byte at a time
over a serial port.  Each item consists of a type followed by the
data.  This program simply wants to print the data.

Suppose one of the types is single precision floating point.
The programmer "knows" that a float is the same size as a long,
and that the data was sent by taking the address of a float on
the other side and just sending out the bytes.

We might see code like this:

	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.

						Tim Smith

ps: of course, I would never do this!



More information about the Comp.lang.c mailing list