Problem with printf()

Tom Ploegmakers tomp at nikhefk.UUCP
Thu Oct 6 22:30:13 AEST 1988


In article <504 at imec.UUCP> croes at imec.UUCP (Kris Croes) writes:
>  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 ?
>

This one has bitten me once too.
The problem is that floats are not passed to functions on the stack, but by
passing a pointer. That is, sometimes, you should check your compiler.

To make it work use:
  printf("%d %f\n",i,(float)i); /*1*/
  printf("%d %f\n",(int)f,f); /*2*/

Suc6.

tom ploegmakers		NIKHEF/K-CSG	(tomp at nikhefk.uucp)	

			po.box 4395, 1009 AJ Amsterdam, the Netherlands.
-- 

tom ploegmakers		NIKHEF/K-CSG	(tomp at nikhefk.uucp)	

			po.box 4395, 1009 AJ Amsterdam, the Netherlands.



More information about the Comp.unix.wizards mailing list