Nude Vicar in Sex Romp!
DAVID NEWALL
ccdn at levels.sait.edu.au
Fri Aug 18 21:02:55 AEST 1989
In article <960 at prlhp1.prl.philips.co.uk>, yuleat at 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 at levels.sait.oz.au
SA Institute of Technology Post: The Levels, South Australia, 5095
More information about the Comp.lang.c
mailing list