Nasty bug

Larry Jones scjones at thor.UUCP
Fri Aug 31 23:24:37 AEST 1990


In article <0093BF08.7F3834E0 at rigel.efd.lth.se>, e89hse at rigel.efd.lth.se writes:
> 
> prnval(s,f)
> char   *s;
> float   f;
> {
>    if(f == 0.0)
>        sscanf(s,"%f",&f);
>    printf("%10.2f\n",f);
> }
> 
> And it didn't work. Why? The answer is that the parameter f is a
> double, not a float since all floats are converted to double when they are
> passed as arguments to functions. Therefore &f is a ptr to double rather than a
> ptr to float as one would expect looking at the declartion.

It is worth noting that different C compilers handle this situation
differently.  Some do exactly what you said, they rewrite your
parameter declaration as double rather than float.  Others assign
the passed double value to a local float which is then used as the
parameter.  If the float and double formats are sufficiently similar,
the compiler can just ignore the second half of the passed double
value and leave the parameter a float without having to make a copy.

ANSI C does not allow the declaration rewriting -- it requires the
compiler to make the parameter have the type you declared it as.
So, this problem should not be a problem much longer, right?
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones at thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
Hello, I'm wondering if you sell kegs of dynamite. -- Calvin



More information about the Comp.lang.c mailing list