home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!inews.Intel.COM!td2cad!dmarer
- From: dmarer@td2cad.intel.com (Dennis Marer)
- Newsgroups: comp.lang.c
- Subject: Unwanted float to double conversion in variable length arg lists?
- Message-ID: <C18924.2yp@inews.Intel.COM>
- Date: 21 Jan 93 23:25:15 GMT
- Sender: news@inews.Intel.COM (USENET News System)
- Organization: Intel Corporation, Santa Clara, CA USA
- Lines: 55
- Nntp-Posting-Host: td2cad
-
- I've got a curious problem when passing floats in a variable length argument
- list...they are automatically converted to doubles, EVEN when I typecast.
- Consider:
-
- #include <stdarg.h>
-
- void func(int num,...) {
- va_list va;
- float x,y;
- int i;
-
- va_start(va,num);
-
- for (i=0; i<num; i++) {
- x = va_arg(va,float);
- y = va_arg(va,float);
- printf("%d: %g %g\n",i,x,y);
- }
-
- va_end(va);
- }
-
-
-
- int main() {
-
-
- func(4,1.0,1.0,2.0,2.0,3.0,3.0,4.0,4.0);
-
- func(2,(float)1.0,(float)1.0,(float)2.0,(float)2.0);
-
-
- }
-
-
- The output of which is: The output I assumed I'd get is:
-
- 0: 1 0 0: 1 1
- 1: 1 0 1: 2 2
- 2: 2 0 2: 3 3
- 3: 2 0 3: 4 4
- 0: 1 0 0: 1 1
- 1: 1 0 1: 2 2
-
-
- I have explicitly typecast a real number to (float), and it still ends
- up on the stack as a double! This is the same on various compilers,
- including Gnu C and IBM C for the AIX operating system. This seems incorrect
- to me, and I'm afraid that if I assume it how things are supposed to be
- implemented, I'll have portability problems.
-
- Is this correct? Are all ANSI C compilers implemented this way? --> Why?
-
- Dennis
- dmarer@td2cad.intel.com
-