What is the difference between “int main()” and “int main(void)” in C/C++?

What is the difference?

In C++, there is no difference, both are same.

Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter.
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters. For example, try to compile and run following two C programs (remember to save your files as .c). Note the difference between two signatures of fun().

 

In C/C++, "int main()" and "int main(void)" both serve as the entry point of a program, where the program execution starts. However, there is a subtle difference between the two in terms of their parameter declaration.

In "int main()", the empty parentheses indicate that the main function can take any number of arguments, but the program does not use them. This means that the programmer can pass command-line arguments to the program, and they will be available to the program through the argc and argv parameters.

In contrast, "int main(void)" specifies that the main function takes no arguments. This means that the program cannot accept any command-line arguments. It is equivalent to "int main()", but it explicitly tells the compiler that the function does not take any arguments.

Both forms are valid and will work for simple programs. However, some programming styles and standards may prefer one over the other. For example, some coding guidelines may require the use of "int main(void)" to make the program more readable and to explicitly show that the function does not accept any arguments. Others may prefer "int main()" because it allows for more flexibility in case the program needs to take arguments in the future.

Submit Your Programming Assignment Details