本文介绍了为什么从main()显式返回0被认为是好的习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了加速的C ++(似乎是一本很棒的书),最后作者说

I've just read the first chapter of Accelerated C++ (seems like an awesome book), and at the end the author says

为什么这被认为是好的做法?在C99中,我总是使用 exit()表示异常程序终止,而省略了 return 0 ,并且从未错过显式返回.

Why is this considered good practice? In C99, I always omitted the return 0, using exit() to signal abnormal program termination, and never missed the explicit return.

推荐答案

在C99和C ++中,如果程序的执行到达 main()函数的右括号,则隐式返回0; 被执行.在C90中不是这种情况-在没有明确的 return 的情况下到达 main()的末尾将导致返回不确定的值(严格来说,行为是不确定的)).

In C99 and in C++ if execution of the program reaches the closing brace of the main() function then an implicit return 0; is executed. That wasn't the case in C90 - reaching the end of main() without an explicit return would result in an indeterminate value being returned (strictly speaking, the behavior is undefined).

我只能猜测,"Accelerated C ++"的作者认为显式返回是一种好习惯,仅仅是因为它使您的意图变得显式.我能想到的唯一另一个原因是它使代码与C90兼容,但是我很难相信这会成为一个重要问题.

I can only guess that the authors of "Accelerated C++" feel that the explicit return is good practice simply because it makes your intent explicit. The only other reason I can think of is that it makes code compatible with C90, but I find it difficult to believe that that would hold much weight as a reason.

这篇关于为什么从main()显式返回0被认为是好的习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:41