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) {...}
mean? And, what does void stand for ?
I've looked here but it's somehow a different question .
int main represents that the function returns some integer even '0' at the end of the program execution. '0' represents the successful execution of a program. int main(void) represents that the function takes NO argument.
int main() indicates that the main function can be called with any number of parameters or without any parameter. On the other hand, int main(void) indicates that the main function will be called without any parameter #include <stdio.h> int main() { static int i = 5; if (--i){ printf("%d ", i); main(10); } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With