Path: utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!mit-eddie!bloom-beacon!eru!hagbard!sunic!news.funet.fi!hydra!kreeta!wirzeniu From: wirzeniu@cs.Helsinki.FI (Lars Wirzenius) Newsgroups: comp.lang.c Subject: Re: float to double pain Message-ID: <9919@hydra.Helsinki.FI> Date: 11 Dec 90 12:18:42 GMT References: <4268@ritcsh.cs.rit.edu> Sender: news@cs.Helsinki.FI Organization: University of Helsinki, Department of Computer Science Lines: 46 In article <4268@ritcsh.cs.rit.edu> you write: >check(af1) >float *af1; /* if this is "double," then this program works on most machines*/ >{ > printf("%f\n", *af1); /* and you'd have to change %f to %lf */ >} Here, *af1 in the call to printf is a float that gets promoted to a double since it's used as a non-fixed argument to printf (i.e., as one of those arguments that can vary from one call to another). It is impossible to pass a float as a non-fixed argument. The same applies to old-style functions. These arguments go through "default argument promotion". This is described in K&R-2, Appendix A, Chapter 7.3.2, page 202, third paragraph (at least in my copy, your page number may wary). By the way, %f for printf means double, there isn't any conversion character for float (because of the reasons outlined above). >t(f1) >float f1; >{ > check(&f1); >} For old-style function definitions arguments of type float are silently rewritten to be of type double. So &f1 is actually a pointer to a double, which is inconsistent with check's expectation of a pointer to a float. You need a cast here to make everything work properly. It may be that your program is working only "by coincidence". Using new style function prototypes and definitions would probably take care of your problem. >main() >{ > t(4.0); >} 4.0 is a constant of type double. Since t actually does expect a double, everything should work OK as far as t is concerned.. >If you post a followup, I'll probably not see it because our news disk is >always full. Please send me email... Done. Lars Wirzenius wirzeniu@cs.helsinki.fi wirzenius@cc.helsinki.fi