Home > Backend Development > C++ > 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
Release: 2023-09-03 11:21:09
forward
1758 people have browsed it

在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");
}
Copy after login

Output

This program will be compiled successfully
Copy after login

Example

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

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template