How does the following code work even though the signature of the function in the declaration doesn't match with the definition? The function declaration has empty parameter list, yet the definition has one parameter. Why the compiler doesn't give error?
#include <stdio.h>
double f(); //function declaration
int main(void)
{
printf("%f\n", f(100.0));
}
double f(double param) //function definition
{
return 5 * param ;
}
It compiles and runs fine (ideone).
But if I change the type of the parameter in the definition, from double
to float
, it gives the following error (ideone):
prog.c:7: error: conflicting types for ‘f’
prog.c:8: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
prog.c:2: error: previous declaration of ‘f’ was here
What is wrong with float
? Why does it give error with float
but not with double
?
Here is the list of pairs of declaration and definition, along with which pair works, and which not:
Works (ideone)
double f(); //declaration double f(double param); //definition
Does not work (ideone)
double f(); //declaration double f(float param); //definition
Works (ideone)
float f(); //declaration float f(double param); //definition
Does not work (ideone)
float f(); //declaration float f(float param); //definition
So as it seems, whenever the parameter-type is float
, it doesn't work!
So I've basically two questions:
- Why does the first example work even though there is a mismatch in the declaration and the definition?
- Why does it not work when the parameter-type is
float
?
I tried understanding the section §6.5.2.2 (C99), but the language is so cryptic that I couldn't clearly understand. I don't even know if I read the correct section. So please explain these behaviors in simple words.
f(float)
passingdouble
value? I think,double
can be converted intofloat
implicitly? Or am I missing something? – Ferraro