sprintf badness

Karl Heuer karl at ima.isc.com
Tue Jan 22 08:22:16 AEST 1991


In article <1991Jan21.004401.15257 at nntp-server.caltech.edu> gceych at iago.caltech.edu writes:
>>  double k;
>>  sscanf(buf,"%4d%7f%4d",&i,&k,&j);
>                  ^^^
>I believe this should be "%7lf", for a DOUBLE number.

Right.  This should fix the problem.

>Same mistake is made in the next line of the program.
>>  printf("i = %d  j = %d  k= %7.2f",i,j,k);

Wrong.  For printf(), "%f" is correct and "%lf" is illegal.  This unfortunate
asymmetry is a result of history and the default widening rules.

(You'll probably find that "%lf" works on your local implementation of
printf().  But since "%f" also works and is Standard, that's the one to use.
Fix it now, before you port to an implementation that works differently.)

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list