home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / os2 / programm / 4353 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  1.4 KB

  1. Path: sparky!uunet!kithrup!hoptoad!decwrl!mips!swrinde!news.dell.com!math.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.os.os2.programmer
  4. Subject: Re: Whats wrong with floor()?
  5. Summary: %lf is not a correct printf() argument specifier
  6. Message-ID: <78104@ut-emx.uucp>
  7. Date: 20 Aug 92 22:09:31 GMT
  8. References: <2118@lunic.luth.se> <1992Aug16.164006.2345@panix.com>
  9. Reply-To: jamshid@emx.utexas.edu
  10. Followup-To: comp.lang.c
  11. Organization: The University of Texas at Austin; Austin, Texas
  12. Lines: 23
  13.  
  14. Followups directed to comp.lang.c.
  15.  
  16. In article <1992Aug16.164006.2345@panix.com> os2man@panix.com (Larry Salomon Jr.) writes:
  17. >>double tmp=33433.740234;
  18. >>printf("%f %f",tmp,floor(tmp));
  19. >>...
  20. >
  21. >This shouldn't have anything to do with it, but %f is for float types.  You
  22. >should be using %lf.
  23.  
  24. That's a common misconception.  "%f" is correct for either float or
  25. double parameters to printf().  Floats convert to double when passed
  26. to a variadic function (eg, printf(const char*, ...)) or to an
  27. unprototyped function.  In fact, ANSI C doesn't define a "%lf" for
  28. printf(), so the following could crash on a very strict compiler:
  29.     double d = 1.2;
  30.     printf("%lf", d);  // error, "%lf" not valid for printf()
  31. Because its such a common error, though, compilers just ignore the 'l'.
  32.  
  33. You *must* properly use %lf or %f for *scanf(), though.
  34.  
  35. Jamshid Afshar
  36. jamshid@emx.utexas.edu
  37.