Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...