Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What was the rationale for making `return 0` at the end of `main` optional?

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:

  1. Understanding why a change was made is helpful in deciding how to use it.
  2. Rationale is frequently included within the standard itself. For example, the C90 standard includes many explanatory footnotes such as footnote 36 which starts, "The intent of this list..."

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.

like image 300
Edward Avatar asked Jul 13 '15 21:07

Edward


People also ask

Why do we write return 0 at the end?

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.

What does returning 0 from the main () function indicate in C?

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.

What does the return 0 statement in main function indicated?

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.

Do I have to return 0 in main?

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.


1 Answers

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.

like image 89
Shafik Yaghmour Avatar answered Oct 14 '22 20:10

Shafik Yaghmour