Harry2o
Harry2o

Reputation: 43

gcc doesn't warn about calling a nullary function WITH parameters?

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-nullary-function-parameters)

Upvotes: 4

Views: 250

Answers (4)

Troubadour
Troubadour

Reputation: 13431

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.

Upvotes: 3

Prasoon Saurav
Prasoon Saurav

Reputation: 92874

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.

Upvotes: 11

anon
anon

Reputation:

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++.

Upvotes: 12

leppie
leppie

Reputation: 117290

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.

Upvotes: 1

Related Questions