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