在`main`的末尾将`return 0'设为可选的理由是什么? [英] What was the rationale for making `return 0` at the end of `main` optional?
问题描述
从C99标准开始,如果main
的末尾没有提供 return ,则要求编译器生成与return 0
或return EXIT_SUCCESS
等效的内容.大约在同一时间,对C ++语言标准也进行了相应且相同的更改.我对这两个原因都很感兴趣,并且我猜想它们不太可能是完全独立且无关的更改.
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.
我的问题是:
此变更的记录依据是什么?
一个理想的答案应该引用C和C ++的权威资料,这就是为什么我用两种语言都标记了这个问题.
An ideal answer would cite authoritative sources for both C and C++ which is why I have tagged the question with both languages.
请注意,与问题不同,在ISO C ++中从main返回0的原因是什么?,我不是在寻求有关是否在程序中编写return 0
的建议,而是在问为什么语言标准本身会被更改.
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:
- 了解进行更改的原因有助于决定如何使用它. 标准中经常包含
- 标准品.例如,C90标准包含许多解释性脚注,例如脚注36开头,此列表的意图..."
在我问这里之前,我已经研究了自己寻找答案的标准,但是没有找到答案.我被要求帮助为一组程序员编写两种语言的编码标准,我想确保我理解为什么存在此功能,以便我可以向其他人准确地解释它的用途.
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.
推荐答案
在新C标准中部分 5.1.2.2.3程序终止作者
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:
到达终止主函数的}会返回值0
reaching the } that terminates the main function returns a value of 0
是:
该标准最终不得不屈从于草率的现有实践.
The standard finally having to bow to sloppy existing practices.
这表明从显式返回main
的值方面的原因是要解决不良的编程习惯.在此之前,返回的状态是不确定的.
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.
他指出,即使在C90中,许多实现也已经实现了这一点,因此这一变化已经反映了通用实现这一事实也可能有所帮助.
He indicates that many implementations already implemented this even in C90, so the fact that this change already reflected common implementation also probably helped.
这篇关于在`main`的末尾将`return 0'设为可选的理由是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!