Can anyone tell me, why in the blazes GCC (e.g. 4.4.3) does not warn about or error the incorrect call of a nullary function?
void hello() {
}
int main( int argc, char* argv[] ) {
int test = 1234;
hello(test);
return 0;
}
(see also http://bytes.com/topic/c/answers/893146-gcc-doesnt-warn-about-calling-nu开发者_JAVA技巧llary-function-parameters)
Because:
void hello() {
does not mean what you think it does. Use:
void hello( void ) {
Without the void, you are saying you can't be bothered to specify the parameters. Note this is one of the many ways that C differs from C++.
In C, void hello()
declares a function hello()
that returns a void
and takes unspecified number of arguments.
Note
In C++ its all together a different scenario. void hello()
in C++ declares a function hello()
that returns a void
and takes no arguments.
From what I can gather from The C Book 4.2 your function definition is not a prototype since it specifies no type information for the arguments. This means the compiler only remembers the return type and retains no information on the arguments whatsoever.
This form of definition is still allowed for backward compatibilty and is not restricted to functions that take no arguments. gcc will equally allow something like
void hello( a ) {
}
int main( int argc, char* argv[] ) {
int test = 1234;
hello(test,1);
return 0;
}
It is only the lack of type information for the arguments that is important here. To fix this and ensure that gcc checks the arguments when the function is used you can put the type information in either a declaration of your function or the definition. Preferably you would put them in both.
All of this still doesn't really answer your question of course as to why gcc doesn't warn you. It must be the case that the gcc team feel there is still enough old-style C code out there to justify suppressing the warning by default. IMO I'm surprised that the -Wstrict-prototype
option as mentioned by @caf is not on by default.
Ha, I had this the other day.
Your definition needs to be:
void hello(void);
Else the function can accept any number of parameters.
But I do understand your point. There is almost no compilers that even give the slightest warning for it.
精彩评论