float to double pain
Lars Wirzenius
wirzeniu at cs.Helsinki.FI
Tue Dec 11 23:18:42 AEST 1990
In article <4268 at 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 at cs.helsinki.fi wirzenius at cc.helsinki.fi
More information about the Comp.lang.c
mailing list