Difference between int main() and int main(void)?

JAN picture JAN · Sep 1, 2012 · Viewed 69.3k times · Source

What does the following mean :

int main(void) {...} 

VS

int main() {...}

?

I think that int main() {...} means that main doesn't receive any parameters (from command line) , however:

int main(int argc, char *argv[])

does.

But what does int main(void) {...} means ? what does the void stands for ?

I've looked here but it's somehow a different question .

Answer

eq- picture eq- · Sep 1, 2012

In C++, there is no difference.


In C, the difference is questionable. Some love to argue that the latter version (the one without void) is technically just a common implementation extension and not guaranteed to work by the standard because of the wording in the standard. However, the standard clearly states that in a function definition an empty set of parameters has a well-defined behaviour: that the function does not take any parameters. Thus such a definition for main matches the following description in the standard:

It [main] shall be defined with a return type of int and with no parameters.

There is, however, a noticeable difference between the two: namely, the version without void fails to provide a correct prototype for the function:

// this is OK.
int main()
{
  if (0) main(42);
}

// this requires a diagnostic to be shown during compiling
int main(void)
{
  if (0) main(42);
}

Oh, and just to be complete: the void has the following meaning in all function declarators:

(6.7.6.3p10) The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.