Problem with printf()

Kris Croes croes at imec.uucp
Wed Oct 5 00:59:25 AEST 1988


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 ?

I am working under Ultrix 2.2, on a VAX-780 and had the same problem on
Apollo. BTW sizeof(int) = sizeof(float) = 4 on both machines.

Some tests (e.g. printing on several lines) showed that the problem is
caused by printing the float under the "%d" format. I know that
normal people don't do such a thing, but that is no reason for printf()
to mess up the stack. (???)

I know that not all binary numbers are valid floats. But if this was
the case, it would give problems in line /*1*/.

Why do things go wrong in line /*2*/ ? Is this a bug in my little
program, in printf(), in C , or where ?

K. Croes
-- 
--------
K. CROES - IMEC - Leuven - Belgium   ..!prlb2!imec!croes

The Demon King sends a "rm -r /" to your shell.



More information about the Comp.unix.wizards mailing list