我应该从 main() 返回 EXIT_SUCCESS 还是 0?
这是一个简单的问题,但我不断看到相互矛盾的答案:C++ 程序的主例程应该返回 0
还是 EXIT_SUCCESS
?
#include <cstdlib>
int main(){return EXIT_SUCCESS;}
或者
int main(){return 0;}
它们是完全相同的东西吗? EXIT_SUCCESS
是否只能与 exit()
一起使用?
我认为 EXIT_SUCCESS
会是一个更好的选择,因为其他软件可能希望将零视为失败,但我也听说如果您返回 0
,编译器能够更改它无论如何,到一个不同的值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
EXIT_FAILURE
,无论是在main
中的 return 语句中还是作为exit()
的参数,是在 C 语言中指示失败的唯一可移植方式或 C++ 程序。例如,exit(1)
实际上可以在 VMS 上发出成功终止的信号。如果您打算在程序失败时使用
EXIT_FAILURE
,那么您也可以在程序成功时使用EXIT_SUCCESS
,只是为了对称性。另一方面,如果程序从不发出失败信号,则可以使用
0
或EXIT_SUCCESS
。标准保证两者都表示成功完成。 (EXIT_SUCCESS
几乎不可能有 0 以外的值,但在我听说过的每个实现中它都等于 0。)使用
0
有一个次要的优势您不需要 C 中的#include
或 C++ 中的#include
(如果您使用的是return
语句而不是调用exit()
)——但是对于任何大小的程序,您都将直接或间接地包含 stdlib。就这一点而言,在从 1999 标准开始的 C 中,以及在所有版本的 C++ 中,到达
main()
的末尾都会隐式地return 0;
,所以您可能不需要显式使用0
或EXIT_SUCCESS
。 (但至少在 C 中,我认为显式的return 0;
是更好的风格。)(有人询问 OpenVMS。我已经很长时间没有使用它了,但我记得奇怪的状态值通常表示成功,而偶数值表示失败。C 实现将
0
映射到1
,因此return 0;
表示成功终止。未改变地传递,因此返回1;
也表示成功终止。)EXIT_FAILURE
, either in a return statement inmain
or as an argument toexit()
, is the only portable way to indicate failure in a C or C++ program.exit(1)
can actually signal successful termination on VMS, for example.If you're going to be using
EXIT_FAILURE
when your program fails, then you might as well useEXIT_SUCCESS
when it succeeds, just for the sake of symmetry.On the other hand, if the program never signals failure, you can use either
0
orEXIT_SUCCESS
. Both are guaranteed by the standard to signal successful completion. (It's barely possible thatEXIT_SUCCESS
could have a value other than 0, but it's equal to 0 on every implementation I've ever heard of.)Using
0
has the minor advantage that you don't need#include <stdlib.h>
in C, or#include <cstdlib>
in C++ (if you're using areturn
statement rather than callingexit()
) -- but for a program of any significant size you're going to be including stdlib directly or indirectly anyway.For that matter, in C starting with the 1999 standard, and in all versions of C++, reaching the end of
main()
does an implicitreturn 0;
anyway, so you might not need to use either0
orEXIT_SUCCESS
explicitly. (But at least in C, I consider an explicitreturn 0;
to be better style.)(Somebody asked about OpenVMS. I haven't used it in a long time, but as I recall odd status values generally denote success while even values denote failure. The C implementation maps
0
to1
, so thatreturn 0;
indicates successful termination. Other values are passed unchanged, soreturn 1;
also indicates successful termination.EXIT_FAILURE
would have a non-zero even value.)不要紧。两者是相同的。
C++ 标准引用:
It does not matter. Both are the same.
C++ Standard Quotes:
根据定义,0 是一个神奇的数字。幸运的是,EXIT_SUCCESS 几乎普遍等于 0。那么为什么不直接返回/退出 0 呢?
退出(EXIT_SUCCESS);意思非常明确。
退出(0);另一方面,在某些方面是违反直觉的。不熟悉 shell 行为的人可能会认为 0 == false == bad,就像 C 中 0 的所有其他用法一样。但事实并非如此 - 在这种特殊情况下,0 == success == good。对于大多数经验丰富的开发人员来说,这不会成为问题。但为什么毫无理由地绊倒新人呢?
tl;dr - 如果你的幻数有一个定义的常量,那么几乎没有理由不首先使用该常量。它更易于搜索,通常更清晰等等,并且不需要您花费任何费用。
0 is, by definition, a magic number. EXIT_SUCCESS is almost universally equal to 0, happily enough. So why not just return/exit 0?
exit(EXIT_SUCCESS); is abundantly clear in meaning.
exit(0); on the other hand, is counterintuitive in some ways. Someone not familiar with shell behavior might assume that 0 == false == bad, just like every other usage of 0 in C. But no - in this one special case, 0 == success == good. For most experienced devs, not going to be a problem. But why trip up the new guy for absolutely no reason?
tl;dr - if there's a defined constant for your magic number, there's almost never a reason not to used the constant in the first place. It's more searchable, often clearer, etc. and it doesn't cost you anything.
这是一个永无止境的故事,反映了“互操作性和可移植性”的局限性(神话)。
程序应该返回什么来指示“成功”应该由接收该值的人(操作系统或调用该程序的进程)而不是由语言规范来定义。
但程序员喜欢以“可移植方式”编写代码,因此他们为定义要返回的符号值的“操作系统”概念发明了自己的模型。
现在,在多对多场景中(多种语言为许多系统编写程序),“成功”的语言约定与操作系统约定(没有人可以保证始终相同)之间的对应关系应该由特定目标平台的库的特定实现来处理。
但是 - 不幸的是 - 这些概念在部署 C 语言(主要是编写 UNIX 内核)时并不那么清楚,并且有大量的书籍写着“返回 0 意味着成功”,因为这在操作系统上是正确的那个时候有一个C编译器。
从那时起,关于如何处理此类信件就没有制定明确的标准化。 C 和 C++ 有自己的“返回值”定义,但没有人授予适当的操作系统翻译(或者更好:没有编译器文档对此有任何说明)。如果对于 UNIX - LINUX 和 - 出于独立原因 - 对于 Windows 为真,0 表示成功,这涵盖了 90% 的现有“消费者计算机”,在大多数情况下 - 忽略返回值(因此我们可以讨论了几十年,但没有人会注意到!)
在这种情况下,在做出决定之前,问以下问题:
- 我是否有兴趣向来电者传达有关我现有情况的信息? (如果我总是返回 0 ...那么所有事情背后就没有任何线索)
- 我的来电者是否有关于此通信的约定? (请注意,单个值不是约定:不允许任何信息表示)
如果这两个答案都是“否”,那么最好的解决方案可能是根本不编写主返回语句。 (并让编译器来决定,关于目标正在工作)。
如果没有约定,0=成功满足大多数情况(如果引入约定,使用符号可能会出现问题)。
如果约定到位,请确保使用与其一致的符号常量(并确保平台之间的约定一致性,而不是值一致性)。
This is a never ending story that reflect the limits (an myth) of "interoperability and portability over all".
What the program should return to indicate "success" should be defined by who is receiving the value (the Operating system, or the process that invoked the program) not by a language specification.
But programmers likes to write code in "portable way" and hence they invent their own model for the concept of "operating system" defining symbolic values to return.
Now, in a many-to-many scenario (where many languages serve to write programs to many system) the correspondence between the language convention for "success" and the operating system one (that no one can grant to be always the same) should be handled by the specific implementation of a library for a specific target platform.
But - unfortunatly - these concept where not that clear at the time the C language was deployed (mainly to write the UNIX kernel), and Gigagrams of books where written by saying "return 0 means success", since that was true on the OS at that time having a C compiler.
From then on, no clear standardization was ever made on how such a correspondence should be handled. C and C++ has their own definition of "return values" but no-one grant a proper OS translation (or better: no compiler documentation say anything about it). 0 means success if true for UNIX - LINUX and -for independent reasons- for Windows as well, and this cover 90% of the existing "consumer computers", that - in the most of the cases - disregard the return value (so we can discuss for decades, bu no-one will ever notice!)
Inside this scenario, before taking a decision, ask these questions:
- Am I interested to communicate something to my caller about my existing? (If I just always return 0 ... there is no clue behind the all thing)
- Is my caller having conventions about this communication ? (Note that a single value is not a convention: that doesn't allow any information representation)
If both of this answer are no, probably the good solution is don't write the main return statement at all. (And let the compiler to decide, in respect to the target is working to).
If no convention are in place 0=success meet the most of the situations (and using symbols may be problematic, if they introduce a convention).
If conventions are in place, ensure to use symbolic constants that are coherent with them (and ensure convention coherence, not value coherence, between platforms).
一旦您开始编写可以返回无数退出状态的代码,您就开始
#define
'所有这些退出状态。在这种情况下,EXIT_SUCCESS
在不是“幻数”。这使您的代码更具可读性,因为所有其他退出代码都将为EXIT_SOMETHING
。如果您只是编写一个在完成后返回的程序,则return 0
是有效的,而且可能更简洁,因为它表明没有复杂的返回代码结构。Once you start writing code that can return a myriad of exit statuses, you start
#define
'ing all of them. In this caseEXIT_SUCCESS
makes sense in context of not being a "magic number". This makes your code more readable because every other exit code will beEXIT_SOMETHING
. If you simply write a program that will return when it's done,return 0
is valid, and probably even cleaner because it suggests that there's no sophisticated return code structure.从程序中返回的只是一个约定。
不,我想不出任何情况下“EXIT_SUCCESS”不会为“0”。
就我个人而言,我推荐“0”。
恕我直言...
What you return from a program is just a convention.
No, I can't think of any circumstances where "EXIT_SUCCESS" wouldn't be "0".
Personally, I'd recommend "0".
IMHO...
tl;dr:你可以不返回任何东西。
不明确地可以(Bjarne Stroustrup这么说......) - 从
main()
函数返回任何内容。如果您的操作系统需要返回值,编译器会处理这些事情。这并不是为了分散 @KeithThompson 或 @EmilioGaravaglia 关于不同可能返回值的情况,并了解程序的返回值如何被解释等。
tl;dr: You could just not return anything.
It is ok (Bjarne Stroustrup says so...) to not-explicitly-return anything from the
main()
function. The compiler will take care things if your OS expects a return value.This is not to detract from the discussion in the answers by @KeithThompson or @EmilioGaravaglia regarding the case of different possible return value, and being aware of how your program's return value gets interpreted etc.
如果您使用 EXIT_SUCCESS,您的代码将更加可移植。
http://www.dreamincode.net/论坛/主题/57495-return-0-vs-return-exit-success/
If you use EXIT_SUCCESS, your code will be more portable.
http://www.dreamincode.net/forums/topic/57495-return-0-vs-return-exit-success/
某些编译器可能会产生问题 - 在 Mac C++ 编译器上,EXIT_SUCCESS 对我来说工作得很好,但在 Linux C++ 编译器上,我必须添加 cstdlib 才能知道 EXIT_SUCCESS 是什么。除此之外,它们是一回事。
Some compilers might create issues with this - on a Mac C++ compiler, EXIT_SUCCESS worked fine for me but on a Linux C++ complier I had to add cstdlib for it to know what EXIT_SUCCESS is. Other than that, they are one and the same.