1

In C, if a function is defined with 0 arguments (empty parameter list) is called with any number of arguments, gcc doesn't give any error. Why is this so?

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
  • 2
    Compile with all warnings enabled (-Wall). – Jabberwocky Mar 15 '16 at 08:03
  • And plese show an [MCVE](http://stackoverflow.com/help/mcve). – Jabberwocky Mar 15 '16 at 08:04
  • Because that is the way the language was designed. Are you asking why the language was designed like that? – juanchopanza Mar 15 '16 at 08:06
  • Compiling with `-Wall` you'll have: _error: void value not ignored as it ought to be_ – LPs Mar 15 '16 at 08:07
  • The C specification says that a function declared without any argument (not even a `void` argument) takes an unspecified number of arguments. If you want to explicitly say that a function takes no arguments, you need to specify a single `void` argument type without a name. – Some programmer dude Mar 15 '16 at 08:09

3 Answers3

2

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.

Mohit Jain
  • 29,414
  • 8
  • 65
  • 93
2

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.


Note:

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.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
-2

Consider this piece of code:

//void Test(int a);

int main()
{
  int a = 0;
  Test();
  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.

Jabberwocky
  • 40,411
  • 16
  • 50
  • 92