[comp.sys.mac] Possible new bug in Think C 4.0 sprintf

jdm@boulder.Colorado.EDU (James D. Meiss) (10/23/89)

	After much hair pulling, I localized a problem
in a program that worked fine under Think C 3, but was
giving difficulties in the new version, using the ANSI
library.

	The problem seems to be the following: Call
sprintf with an unitialized double:

main()
{
	double z;
	char aStr[80];

	/* z = 0.0   <==== include this to prevent crash*/
....
	sprintf(aStr, ".13lf",z);
....
}

	Now you may think this is a stupid thing to do, I'll
probably agree to that, but I don't think it should cause an
odd address error. BTW it doesn't crash in sprintf, but memory
seems to be trashed, and it crashes soon afterwards.

	Has someone else seen this? Is there a fix, other than 
initializing the variable?
									
		Jim Meiss	
		jdm@euclid.Colorado.edu

jnh@ecemwl.ncsu.edu (Joseph N. Hall) (10/24/89)

In article <13068@boulder.Colorado.EDU> jdm@boulder.Colorado.EDU (James D. Meiss) writes:
>
>	After much hair pulling, I localized a problem
>in a program that worked fine under Think C 3, but was
>giving difficulties in the new version, using the ANSI
>library.
>
>	The problem seems to be the following: Call
>sprintf with an unitialized double:
>
>main()
>{
>	double z;
>	char aStr[80];
>
>	/* z = 0.0   <==== include this to prevent crash*/
>....
>	sprintf(aStr, ".13lf",z);
>....
>}

From p.329, "C: A Reference Manual," by Harbison and Steele:

	... Draft Proposed ANSI C uses the L (uppercase letter l)
	prefix with e, E, f, F, g, and G to indicate that the
	argument has type long double ...

From p.334:

f	Signed decimal floating-point conversion is performed.  One
	argument is consumed, which should be of type double.  (Note
	that if an argument of type float is given, it is converted to
	type double by the usual function argument conversions before
	printf ever sees it, so it does work to use %f to print a
	number of type float.)
	...
	...The l (long size) specification is not relevant to the f
	conversion operation.

You are using the specifier "%lf," which is nonstandard.  The correct
specifier for BOTH float and double is "%f".  Last I heard there was no
portable use for "%lf" and it is NOT standard "C."

I don't remember whether "%lf" is a compatibility hack for 80- or
96-bit extended types; it might be.  On the other hand, the %lf specifier
might just be totally ignored ... in any event what you're doing is
incorrect and should result in some kind of an error.


v   v sssss|| joseph hall                      || 4116 Brewster Drive
 v v s   s || jnh@ecemwl.ncsu.edu (Internet)   || Raleigh, NC  27606
  v   sss  || SP Software/CAD Tool Developer, Mac Hacker and Keyboardist
-----------|| Disclaimer: NCSU may not share my views, but is welcome to.