Since the definition
int main() { }
does not include a prototype; it does not indicate the number or type of parameters.
It:
int main(void) { }
contains a prototype.
With empty parentheses, you say that main accepts a fixed but unspecified number and type of arguments. With (void) you explicitly say that it does not accept any arguments.
On the first call:
main(42);
will not necessarily be diagnosed.
This goes back to the days leading up to ANSI before prototypes were introduced into the language, and most functions were defined with empty parentheses. Then it was completely legal to write:
int foo(); int foo(n) int n; { } ... foo(42);
When prototypes were added to the language (borrowed from C ++), it was necessary to preserve the old meaning of empty parentheses; the "new" (it was 1989) syntax (void) was added, so you can explicitly say that the function takes no arguments.
(C ++ has different rules, it does not allow the use of non-prototyped old-style functions, and empty parentheses mean that the function takes no arguments. C ++ allows the syntax (void) for compatibility with C, but it is usually not recommended.)
It is best to use (void) because it is more explicit. It is not clear that the form int main() even valid, but I have never seen a compiler that does not accept it.
source share