“int main()” Vs. “int main(void)”: Know the Difference Between “int main()” and “int main(void)” in C/C++
Both of these are types of the main() function, but there is a fundamental difference between int main() and int main(void). In this article, we will discuss the same in a tabular form. But let us first know a bit more about main().
Various programs consist of both- entry points as well as exit points. Here, the entry point refers to that point from where the execution of that program begins. The exit point, on the other hand, refers to that point where that program terminates its overall execution.
In the case of C, the operating system (OS) uses the main() function for determining the starting point for running and executing a code. Thus, the main() function is a prerequisite to every program. This function, just like the C function, consists of a definition. Here, the int main(), the void main(), and the void main() are basically the function definitions of the main(). Let us get into the details of int main() and int main(void).
What is “int main()” in C/C++?
The int here acts as a type of a return main. In other words, the function gets to expect a value of the return type int (integers) passing through it. It also consists of a return statement. It returns its 0 value ideally to the main one after we complete a program.
The primary purpose of returning the value 0 into the main is to make the OS understand that the execution of the program is now successful. For the OS, a return of 0 refers to successful execution. Anything else here means that a problem might have occurred.
It is very useful if the execution of program B depends a lot on the successful program A execution. In such a case, the use of the return value OS decides and identifies if we should proceed ahead with program B execution or not.
As a matter of fact, we need to make use of the int main() in the Unix-based systems as our main function. It is because the Unix OS expects some return value for determining the program’s successful execution.
What is “int main(void)” in C/C++?
The definition mentioned above for int main() is similar for the int main(void) as well. But there is only one difference here. The number of arguments that we can pass is null to main in this case. Thus, when the main function doesn’t take any of the arguments, it basically uses the int main(void).