Sometimes we see two types of main function definitions. int main() and int main(void). So what's the difference?
In C, there is no difference. In C, both are correct. But the second way of writing is technically better. It specifies that the function does not accept any parameters. In C, if a function does not specify parameters, then it can be called with no parameters or with any number of parameters. Please check these two codes. (Remember these are C code, not C code)
#include<stdio.h> void my_function() { //some task } main(void) { my_function(10, "Hello", "World"); }
This program will be compiled successfully
#include<stdio.h> void my_function(void) { //some task } main(void) { my_function(10, "Hello", "World"); }
[Error] too many arguments to function 'my_function'
In C, both programs will fail. Therefore, we can understand that in C, int main() can take any number of parameters. But int main(void) does not allow any parameters.
The above is the detailed content of In C/C++, what is the difference between 'int main()' and 'int main(void)'?. For more information, please follow other related articles on the PHP Chinese website!