Nasty bug

Stan Brown, Oak Road Systems browns at iccgcc.decnet.ab.com
Fri Aug 31 04:21:03 AEST 1990


In article <2117 at krafla.rhi.hi.is>, pierre at rhi.hi.is (Kjartan Pierre Emilsson) writes:
> From article <0093BF08.7F3834E0 at rigel.efd.lth.se>, by e89hse at rigel.efd.lth.se:
>> 
>> [Code deleted] 
>>	prnval(s,f)
>>	char *s;
>>	float 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 float.
>> 
>>  Henrik Sandell
> 
> I don't know what other think, but as a relatively experienced C programmer, I
> find this bug very counter-intuitive, [...]

This isn't a bug--or at least it's not a compiler bug; it's the way the
language is designed to work.  Page 184, sec 6.2 of K&R 1: "whenever a float
appears in an expression it is lengthened to double. ..."  

But I think the real moral of the story is, always use prototypes.  The code
shown above doesn't allow the compiler to do type checking.  If the function
were defined as
	int prnval(char *s, float f)
	{
	    ...
then the compiler is not just able but _required_ to type-check the arguments.
(I think the original post had the function in the same source file and
preceding the code of the function that called it.  If that's not the case,
then the prototype declaration would be need to be in scope.)

Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.         (216) 371-0043
The opinions expressed are mine. Mine alone!  Nobody else is responsible for
them or even endorses them--except my cat Dexter, and he signed the power of
attorney only under my threat to cut off his Cat Chow!



More information about the Comp.lang.c mailing list