Home  >  Article  >  Backend Development  >  In C/C++, what is the difference between "int main()" and "int main(void)"?

In C/C++, what is the difference between "int main()" and "int main(void)"?

PHPz
PHPzforward
2023-09-03 11:21:091617browse

在C/C++中,“int main()”和“int main(void)”之间的区别是什么?

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)

Example

#include<stdio.h>
void my_function() {
   //some task
}
main(void) {
   my_function(10, "Hello", "World");
}

Output

This program will be compiled successfully

Example

#include<stdio.h>
void my_function(void) {
   //some task
}
main(void) {
   my_function(10, "Hello", "World");
}

Output

[Error] too many arguments to function &#39;my_function&#39;

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete