First off let us forget about main. In C(not C++) if you define a function with no parameters like this
int f(){ return 0;}
It is legal to call such a function with any number of arguments:
int a = f(); /* legal */
int a = f("help", 1, 2.0); /* legal */
If you want your function f
to only work with exactly no arguments you can amend it like this:
int f(void){return 0;}
int a = f(); /* legal */
int a = f("help", 1, 2.0); /* not legal as it has too many parameters */
The same thing applies to main()
and main(void)
. In most cases in the reasonable world most people would never care however I have encountered legal code that calls main
within the program.
So declaring main
like:
int main() {
/* Do a bunch of stuff here */
}
Allows for code elsewhere in your program to do this:
main();
main(1,2,3,4);
By declaring main(void)
you add a compiler check that prevents the latter example main(1,2,3,4)
from compiling.