What is the difference between void main and int main in C/C++?
What is the difference between void main and int main in C/C++?
225
07-Jul-2023
Updated on 10-Jul-2023
Aryan Kumar
10-Jul-2023In C++, the
main()function is the entry point for all C++ programs. It is the first function that is called when the program starts executing. Themain()function can have two different return types:voidorint.void main()indicates that themain()function does not return any value. This is the most common way to define themain()function.int main()indicates that themain()function returns an integer value. This value can be used by the operating system to determine the status of the program. For example, if the program terminates normally, themain()function will return the value0. If the program terminates abnormally, themain()function will return a non-zero value.It is important to note that the
main()function must always have a return type. Even if you do not want themain()function to return any value, you should still use thevoidreturn type.Here are some examples of how to define the
main()function:C++
In general, you should use
void main()unless you have a specific reason to useint main().