home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13426 < prev    next >
Encoding:
Internet Message Format  |  1992-09-09  |  2.3 KB

  1. Path: sparky!uunet!pipex!unipalm!uknet!comlab.ox.ac.uk!oxuniv!atmtjkv
  2. From: atmtjkv@vax.oxford.ac.uk
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help!!! Please Read this message!?!?!?!?!...
  5. Message-ID: <1992Sep9.085352.8755@vax.oxford.ac.uk>
  6. Date: 9 Sep 92 07:53:52 GMT
  7. References: <mcook.715979137@melkur.dev.cdx.mot.com> <1992Sep9.043345.15040@nuscc.nus.sg>
  8. Organization: Oxford University VAX 6620
  9. Lines: 48
  10.  
  11. In article <1992Sep9.043345.15040@nuscc.nus.sg>, eletanjm@nuscc.nus.sg (TAN JIN MENG) writes:
  12. > mcook@melkur.dev.cdx.mot.com (Michael Cook) writes:
  13. > : eletanjm@nuscc.nus.sg (TAN JIN MENG) writes:
  14. > : 
  15. > : >gmr6139@ultb.isc.rit.edu (G.M. Richard ) writes:
  16. > : >: I can't get my compiler to do float-type variables.
  17. > : 
  18. > : ...
  19. > : 
  20. > : >: #define apples  4
  21. > : >: #define oranges 3.6
  22. > : >:  
  23. > : >: main()
  24. > : >: {
  25. > : >:         printf("\nApples = %d\nOranges = %f", apples, oranges);
  26. > : >: }
  27. > : 
  28. > : >Your compiler can't know what orange is... So it assumes that its an
  29. > : >int. That's why the wrong result. Alternatives ...
  30. > : 
  31. > : What?  Don't be silly...  The compiler knows what orange is.
  32. > : 
  33. > : Matt's problem is probably due to Turbo C trying to avoid linking against the
  34. > : floating point library (so as to keep the program smaller).  The compiler saw
  35. > : no "float" or "double" declarations, and so it linked against the non-floating
  36. > : point version of the standard library, and picked-up the non-floating point
  37. > : version of printf, which probably doesn't understand %f.
  38. > If the printf doesn't understand %f, it will print "%f" verbatim and not
  39. > even attempt to interpret it.
  40. > jin meng
  41.  
  42. Isn't the point rather that oranges is passed as double (vide K&R I p186):
  43.     `Any actual arguments of type float are converted to double before the call....'
  44. so there's a double on the stack.  When printf is called, the format string tells
  45. it how to interpret the stack frame.  It takes apples off as an int, correctly,
  46. because of the %d, but it tries to interpret oranges as a float (%f), when in fact
  47. it has been passed as a double.  So the results will be unpredictable, and will vary from
  48. compiler to compiler. It is possible that some compilers may trap printfs, and look at
  49. the format string at compile time, but it isn't required!
  50.  
  51. -- 
  52. Tim Kingsmill-Vellacott
  53. Atmospheric, Oceanic and Planetary Physics
  54. University of Oxford, UK.
  55. tjkv@atm.ox.ac.uk  preferred
  56.