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

  1. Path: sparky!uunet!olivea!inews.Intel.COM!td2cad!dmarer
  2. From: dmarer@td2cad.intel.com (Dennis Marer)
  3. Newsgroups: comp.lang.c
  4. Subject: Unwanted float to double conversion in variable length arg lists?
  5. Message-ID: <C18924.2yp@inews.Intel.COM>
  6. Date: 21 Jan 93 23:25:15 GMT
  7. Sender: news@inews.Intel.COM (USENET News System)
  8. Organization: Intel Corporation, Santa Clara, CA USA
  9. Lines: 55
  10. Nntp-Posting-Host: td2cad
  11.  
  12. I've got a curious problem when passing floats in a variable length argument
  13. list...they are automatically converted to doubles, EVEN when I typecast.
  14. Consider:
  15.  
  16. #include <stdarg.h>
  17.  
  18. void func(int num,...) {
  19.     va_list va;
  20.     float x,y;
  21.     int i;
  22.  
  23. va_start(va,num);
  24.  
  25. for (i=0; i<num; i++) {
  26.     x = va_arg(va,float);
  27.     y = va_arg(va,float);
  28.     printf("%d: %g %g\n",i,x,y);
  29.     }
  30.  
  31. va_end(va);
  32. }
  33.  
  34.  
  35.  
  36. int main() {
  37.  
  38.  
  39. func(4,1.0,1.0,2.0,2.0,3.0,3.0,4.0,4.0);
  40.  
  41. func(2,(float)1.0,(float)1.0,(float)2.0,(float)2.0);
  42.  
  43.  
  44. }
  45.  
  46.  
  47. The output of which is:        The output I assumed I'd get is:
  48.  
  49. 0: 1 0                0: 1 1
  50. 1: 1 0                1: 2 2
  51. 2: 2 0                2: 3 3
  52. 3: 2 0                3: 4 4
  53. 0: 1 0                0: 1 1
  54. 1: 1 0                1: 2 2
  55.  
  56.  
  57. I have explicitly typecast a real number to (float), and it still ends 
  58. up on the stack as a double!  This is the same on various compilers,
  59. including Gnu C and IBM C for the AIX operating system.  This seems incorrect
  60. to me, and I'm afraid that if I assume it how things are supposed to be
  61. implemented, I'll have portability problems.
  62.  
  63. Is this correct? Are all ANSI C compilers implemented this way?  --> Why?
  64.  
  65.                 Dennis
  66.                 dmarer@td2cad.intel.com
  67.