home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20317 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.5 KB  |  54 lines

  1. Path: sparky!uunet!think.com!rpi!usc!srhqla!quest!kdq
  2. From: kdq@quest.UUCP (Kevin D. Quitt)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Unwanted float to double conversion in variable length arg lists
  5. Message-ID: <3ukyXB4w165w@quest.UUCP>
  6. Date: Mon, 25 Jan 93 16:36:13 PST
  7. References: <C18924.2yp@inews.Intel.COM>
  8. Reply-To: srhqla!quest!kdq
  9. Organization: Job quest  (805) 251-8210,  So Cal: (800) 400-8210
  10. Lines: 42
  11.  
  12. dmarer@td2cad.intel.com (Dennis Marer) writes:
  13.  
  14. > I've got a curious problem when passing floats in a variable length argument
  15. > list...they are automatically converted to doubles, EVEN when I typecast.
  16. > Consider:
  17. > #include <stdarg.h>
  18. > void func(int num,...) {
  19. ...
  20. > }
  21. > int main() {
  22. > func(4,1.0,1.0,2.0,2.0,3.0,3.0,4.0,4.0);
  23. > func(2,(float)1.0,(float)1.0,(float)2.0,(float)2.0);
  24. > }
  25. > I have explicitly typecast a real number to (float), and it still ends 
  26. > up on the stack as a double!
  27.  
  28.     Yep.  Casting something as float does not prevent the automatic
  29. promotion to double when no function prototype is in scope.  Since
  30. your function func does not specify floats, C *must* convert to
  31. doubles.  Make sense?
  32.  
  33. ffunc( float foo )
  34. {
  35. ...
  36. }
  37. main()
  38.  float f = 3.14;
  39.  
  40.  ffunc( (int)f )
  41. }
  42.     What should the compiler do?  Completely ignore the typecast or
  43. convert f to an int (should get a warning about possible loss of
  44. precision), and then back to a float as the function prototype
  45. requires? 
  46.  
  47.  
  48.  _
  49. Kevin D. Quitt      96.37% of all statistics are made up.     srhqla!quest!kdq
  50.