Problem with printf()

Andrew Koenig ark at alice.UUCP
Sun Oct 9 00:31:27 AEST 1988


In article <504 at imec.UUCP>, croes at imec.uucp (Kris Croes) writes:
> Hello NetLand, 
 
> May I introduce to you...
> A little program containing a big problem.
>   main()
>   {
>   int i = 17;
>   float f = 17.0;
 
>   printf("%d %f\n",i,i); /*1*/
>   printf("%d %f\n",f,f); /*2*/
>   }
 
> Its output is:
> 17 0.000000
> 17032 0.000000
>       ^^^^^^^^ Shouldn't this be 17.00000 ?


A float is automatically cast to double when used as an argument.
Thus f probably takes up two words on the stack and %d picks off
only one.


Why do you want to use %d to print a float anyway?


Incidentally, the %g format is usually more useful than %f.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.unix.wizards mailing list