Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!cs.utexas.edu!uunet!murtoa.cs.mu.oz.au!munnari.oz.au!mimir!hugin!augean!sirius!nt!levels!ccdn From: ccdn@levels.sait.edu.au (DAVID NEWALL) Newsgroups: comp.lang.c Subject: Re: Nude Vicar in Sex Romp! Message-ID: <1182@levels.sait.edu.au> Date: 18 Aug 89 11:02:55 GMT References: <960@prlhp1.prl.philips.co.uk> Organization: Sth Australian Inst of Technology Lines: 36 In article <960@prlhp1.prl.philips.co.uk>, yuleat@prlhp1.prl.philips.co.uk (yuleat) writes: > Now that I've got your attention, why not consider the following > piece of code (which has nothing to do with nude vicars!) : > > [ code deleted ] > > The interesting thing is that on both the compilers (HP & Apollo) > I've tried this on, the values printed out by the printf() are > different. Specifically, x is gibberish, whilst y is correct > (0.1234). I believe that the reason for this is that x in main() > is converted to a double as it is passed to fn1(), thus the pointer > that is passed to fn2() is really a pointer to a double and hence > when it is de-refernced as a pointer to a float, it gets the "wrong" > answer. Page 41, K&R: "Notice that all float's in an expression are converted to double; all floating point arithmetic in C is done in double precision." Page 42, K&R: "Since a function argument is an expression, type conversions also take place when arguments are passed to functions: in partuclar, ... float becomes double. This is why we have declared function arguments to be ... double even when the funciton is called with ... float." The question is not why x in main() is passed as a double, but rather, why the compiler doesn't warn you when you declare x in fn1() as a float. For the record, I get the same result on an NCR Tower 32/400 (Tower OS 2.01) and a Sun 3/60 (SunOS 4.0.3) David Newall Phone: +61 8 343 3160 Unix Systems Programmer Fax: +61 8 349 6939 Academic Computing Service E-mail: ccdn@levels.sait.oz.au SA Institute of Technology Post: The Levels, South Australia, 5095