What should main() return in C and C++?
What should main() return in C and C++?
273
07-Jul-2023
Updated on 10-Jul-2023
Aryan Kumar
10-Jul-2023In C and C++, the
main()function is the entry point for all programs. It is the first function that is called when the program starts executing. Themain()function can return an integer value, which can be used by the operating system to determine the status of the program.main()function should return the value0. This is the most common return value formain().main()function should return a non-zero value. The specific value that is returned can be used by the operating system to indicate the type of error that occurred.For example, the following code shows a
main()function that returns0if the program terminates normally, and1if the program terminates abnormally:C
It is important to note that the
main()function must always return an integer value, even if you do not want to return any specific value. If you do not return an integer value, the compiler will generate an error.In C, the
main()function can also be defined with avoidreturn type. This means that themain()function does not return any value. However, it is still recommended to return0from themain()function, even if the function is defined with avoidreturn type.Here is an example of a
main()function that is defined with avoidreturn type:C
In general, you should return
0from themain()function, even if the function is defined with avoidreturn type. This will help to ensure that the operating system can correctly determine the status of the program.