Why can functions with no arguments defined be called with any number of arguments?


This question already has an answer here:


Because this is how C specs define a function.

If you want a function to take no argument, explicitly specify void in argument list.

If you want a function to take variable number of arguments use variadic arguments .... Or in ancient C you could leave the argument list empty and the compiler would apply default promotion to each argument.

Although the latter is obsolescent now: from section 6.11.6 Function declarators C11 spec n1570 draft

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

TL;DR - int func(void) {.. and int func() {.. are not the same.

  • In first case, by explicitly mentioning void as parameter type (no identifier required)note the function is defined as such there is no input parameter, so while calling it should not pass any argument.

  • In the later case, there is no check made on the number of arguments passed, so you are allowed to pass any number and type of arguments.


As per C11, chapter ยง6.9.1

If the declarator includes a parameter type list, the declaration of each parameter shall include an identifier, except for the special case of a parameter list consisting of a single parameter of type void, in which case there shall not be an identifier. No declaration list shall follow.

Consider this piece of code:

//void Test(int a);

int main()
  int a = 0;
  return 0;

It will compile (mostly with a warning depending on compiler, settings etc.) because the compiler assumes that Test is a function taking any number of arguments and returning an int. This is also called "implicit declaration". You should never use this obsolete feature of the C language.

If you uncomment the first line void Test(int a);, it won't compile any more because now the compiler knows that Test is a function returning an int and taking exactly one int argument and as we call Test() without arguments, the compiler can throw an error because the arguments upon call dont match the arguments of the function when it has been declared.

Now we can ask the question: why is implicit declaration allowed in first place? Well, it's the way the language has been designed. IMO it is a design error.

Need Your Help

Running out of memory when getting a bitmap from a server?

c# memory compact-framework bmp

I'm making an application which uses MANY images. The application gets the images from a server, and downloads them one at a time.

Using Python's CFFI and excluding system headers

python c gcc ctypes python-cffi

I'm trying to use Python's CFFI to develop Python bindings to a scientific model written in C. The CFFI documentation is a little sparse and I'm stuck at the cdef stage.