tags:

views:

87

answers:

4

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)

+1  A: 

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.

leppie
@leppie: I don't follow. Why should the compiler warn about a valid call to a function?
Troubadour
If you add the `-Wstrict-prototypes` flag to gcc, it will warn for this: `x.c:4: warning: function declaration isn't a prototype`
caf
@caf: Thanks :)
leppie
@leppie: Ah, okay, after reading [The C Book 4.2](http://publications.gbdirect.co.uk/c_book/chapter4/function_types.html) I see now. I was confused by your statement about it accepting any number of parameters which made me think the call was valid i.e. I thought it was like using ellipses.
Troubadour
@Troubadour: If there is no `void`, then it is like `...` (with no first parameter).
leppie
@leppie: Except you can't get at the arguments since `...` needs at least one preceding argument to pass to `va_start`. What you mean is that that compiler will let you call it regardless of what you pass to it?
Troubadour
yes, the compiler has no prototype for that function, and it will "accept" any way you call it. How you can access those passed arguments within the func is another matter.
ShinTakezou
+15  A: 

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.

Prasoon Saurav
+10  A: 

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

anon
+3  A: 

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.

Troubadour