Starting with the C99 standard, the compiler is required to generate the equivalent of a return 0
or return EXIT_SUCCESS
if no return is supplied at the end of main
. There was also a corresponding and identical change to the C++ language standard around that same time. I am interested in the reasons for both and I guessed that it was unlikely they were entirely separate and unrelated changes.
My question is:
What was the documented rationale for this change?
An ideal answer would cite authoritative sources for both C and C++ which is why I have tagged the question with both languages.
Note that unlike the question What the reasons for/against returning 0 from main in ISO C++?, I'm not asking for advice on whether to write return 0
in my programs -- I'm asking why the language standards themselves were changed.
To help understand the purpose for the question, here is a bit more of the context:
I've studied the standards searching for the answer myself before I asked here, but did not find the answer. I've been asked to help write coding standards for both languages for a group of programmers and I wanted to make sure I understand why this feature exists so that I may accurately explain its use to others.
The main function is generally supposed to return a value and after it returns something it finishes execution. The return 0 means success and returning a non-zero number means failure. Thus we "return 0" at the end of main function.
The main function in a C program returns 0 because the main() method is defined and imported first when the code is run in memory.
Return statement in main() function Return uses exit code which is int value, to return to the calling function. Using the return statement in the main function means exiting the program with a status code; for example, return 0 means returning status code 0 to the operating system.
Returning zero from main() does not have to return zero to the host environment. From the C90/C99/C++98 standard document: If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
In The New C Standard section 5.1.2.2.3 Program termination the author Derek Jones commentary on this lines from the C99 standard:
reaching the } that terminates the main function returns a value of 0
is:
The standard finally having to bow to sloppy existing practices.
Which indicates the rationale was to address poor programming practices with respect to explicitly returning a value from main
. Prior to this the status returned was undefined.
He indicates that many implementations already implemented this even in C90, so the fact that this change already reflected common implementation also probably helped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With