Difference between void main and int main
Written by
What is the difference between int main(), void main() and int main(void) ?
Every program has an entry and an exit point. An entry point is from where the program starts its execution, the exit point is where the program would terminate.
Now in C, that entry point from where the Operating system understands that this is the start point for me to run and execute the code, is understood by the main() function.
Hence, every C program has to have a main() function in it. Like C function, the main function also has a definition. Thus, int main() , void main(), int main(void) are nothing but function definitions of main().
Next question that might pop up is, what is the format of these and how are they different?
What is void main()?
Syntax:
#include<stdio.h> //headerfiles
void main() //function with empty() , accept any no.of arguments
{
printf(" Hello World!\n"); //function always enclosed inside {}
}
Output:
Hello World!
In the above syntax, ‘void‘ is the return type of the function. void means null in C. Hence the function does not return any value to the Operating system after its execution, that is on exit.
Next is the name of the function which is ‘main’.
Since no value is returned back to the Operating system, there is no method to know if the program ran successfully or not.
What is int main()?
Syntax:
#include<stdio.h> //headerfiles
int main()
{
printf(" Hello World!\n");
return 0; //return statement returning value 0 as return type is int
}
Output:
Hello World!
Here, the return type of main is int. That is the function ideally expects a return type value int (integer) to be passed to it. Hence, the return statement; which returns value 0 to main on completion of the program.
In C, empty functions (), accept any number of arguments; i.e values passed to the function. In this case, any number of arguments will be accepted by the function.
The purpose of returning a value 0 to main is for the operating system to understand that the program has executed successfully. For the operating system, a return of 0 means successful execution and anything else means there was a problem.
This is useful if a program B’s execution depends on the successful execution of program A; in that case, using the return value OS identifies and decides whether to proceed with execution of B or not.
Also, for Unix based systems you would have to use int main() as your main function; as the Unix OS expects a return value in order to determine the successful execution of the program.
What is int main(void)?
Syntax:
#include<stdio.h> //headerfiles
int main(void) // accepts no arguments as void
{
printf(" Hello World!\n");
return 0;
}
Output:
Hello World!
The above definition is similar to int main(), with only one change; the number of arguments that can be passed is now null to main.
Hence when your main function is not taking any argument, it is preferable to use int main(void).
Which amongst the above should be ideally used and which should be avoided (int main() Vs void main() Vs int main(void))?
To summarize kindly note the following points:
- void main() type should be avoided since this type is acceptable only on IDE running on a Windows-based system. It is also not according to programming standards for C as OS does not know if the program successfully executed or not.
- int main() and int main(void): These two are the preferred type as they are as per standards, the OS knows the program state and it compiles on both Unix based and Windows-based IDE. int main(void) should be ideally used, as main generally does not take any argument.