C++ 我应该从 main() 返回 EXIT_SUCCESS 还是 0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8867871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:15:22  来源:igfitidea点击:

Should I return EXIT_SUCCESS or 0 from main()?

c++creturn-valuemain

提问by Trevor Hickey

It's a simple question, but I keep seeing conflicting answers: should the main routine of a C++ program return 0or EXIT_SUCCESS?

这是一个简单的问题,但我一直看到相互矛盾的答案:C++ 程序的主例程应该返回0还是EXIT_SUCCESS

#include <cstdlib>
int main(){return EXIT_SUCCESS;}

or

或者

int main(){return 0;}

Are they the exact same thing? Should EXIT_SUCCESSonly be used with exit()?

它们是完全一样的吗?应该EXIT_SUCCESS只与exit()?

I thought EXIT_SUCCESSwould be a better option because other software may want to deem zero as failure, but I also heard that if you return 0, the compiler is capable of changing it to a different value anyway.

我认为EXIT_SUCCESS这是一个更好的选择,因为其他软件可能希望将零视为失败,但我也听说如果您 return 0,编译器无论如何都能够将其更改为不同的值。

回答by Keith Thompson

EXIT_FAILURE, either in a return statement in mainor as an argument to exit(), 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.

EXIT_FAILURE,无论是在 的 return 语句中main还是作为 的参数exit(),都是指示 C 或 C++ 程序失败的唯一可移植方式。 exit(1)例如,实际上可以在 VMS 上发出成功终止的信号。

If you're going to be using EXIT_FAILUREwhen your program fails, then you might as well use EXIT_SUCCESSwhen it succeeds, just for the sake of symmetry.

如果你打算EXIT_FAILURE在你的程序失败时使用,那么你最好EXIT_SUCCESS在它成功时使用,只是为了对称。

On the other hand, if the program never signals failure, you can use either 0or EXIT_SUCCESS. Both are guaranteed by the standard to signal successful completion. (It's barely possible that EXIT_SUCCESScould have a value other than 0, but it's equal to 0 on every implementation I've ever heard of.)

另一方面,如果程序从不发出失败信号,您可以使用0EXIT_SUCCESS。标准保证两者都表示成功完成。(几乎不可能EXIT_SUCCESS有 0 以外的值,但在我听说过的每个实现中它都等于 0。)

Using 0has the minor advantage that you don't need #include <stdlib.h>in C, or #include <cstdlib>in C++ (if you're using a returnstatement rather than calling exit()) -- but for a program of any significant size you're going to be including stdlib directly or indirectly anyway.

Using0具有#include <stdlib.h>在 C 或#include <cstdlib>C++ 中不需要的次要优势(如果您使用的是return语句而不是调用exit())——但是对于任何重要大小的程序,您将直接或间接地包含 stdlib反正。

For that matter, in C starting with the 1999 standard, and in all versions of C++, reaching the end of main()does an implicit return 0;anyway, so you might not need to use either 0or EXIT_SUCCESSexplicitly. (But at least in C, I consider an explicit return 0;to be better style.)

就此而言,在从 1999 标准开始的 C 中,以及在所有版本的 C++ 中,无论如何都会达到main()隐式的末尾return 0;,因此您可能不需要使用0EXIT_SUCCESS显式使用。(但至少在 C 中,我认为显式return 0;是更好的风格。)

(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 0to 1, so that return 0;indicates successful termination. Other values are passed unchanged, so return 1;also indicates successful termination. EXIT_FAILUREwould have a non-zero even value.)

(有人问过 OpenVMS。我很久没有使用它了,但我记得奇数状态值通常表示成功,而偶数值表示失败。C 实现映射01,因此return 0;表示成功终止。其他值不变地传递,因此return 1;也表示成功终止。EXIT_FAILURE将具有非零偶数值。)

回答by Alok Save

It does not matter. Both are the same.

没关系。两者都是一样的。

C++ Standard Quotes:

C++ 标准引用:

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.

如果状态值为零或 EXIT_SUCCESS,则返回状态成功终止的实现定义形式。

回答by James

0 is, by definition, a magic number. EXIT_SUCCESS is almost universally equal to 0, happily enough. So why not just return/exit 0?

根据定义,0 是一个幻数。EXIT_SUCCESS 几乎普遍等于 0,很高兴。那么为什么不直接返回/退出 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?

退出(0);另一方面,在某些方面是违反直觉的。不熟悉 shell 行为的人可能会假设 0 == false == bad,就像 C 中 0 的所有其他用法一样。但不是 - 在这种特殊情况下,0 == 成功 == 好。对于大多数有经验的开发人员来说,这不是问题。但是为什么要毫无理由地绊倒这个新人呢?

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.

tl;dr - 如果你的幻数有一个定义的常量,那么几乎没有理由不首先使用这个常量。它更易于搜索,通常更清晰等,而且不会花费您任何费用。

回答by Emilio Garavaglia

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.

但是 - 不幸的是 - 这些概念在部署 C 语言(主要是为了编写 UNIX 内核)时并不那么清楚,并且在大量书籍中说“返回 0 意味着成功”,因为这在操作系统上是正确的那个时候有一个 C 编译器。

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!)

从那时起,就如何处理这种通信没有明确的标准化。C 和 C++ 对“返回值”有自己的定义,但没有人授予正确的操作系统转换(或者更好:没有编译器文档对此进行任何说明)。0 表示成功,如果对于 UNIX - LINUX 和 - 出于独立原因 - 对于 Windows 也是如此,并且这涵盖了现有“消费者计算机”的 90%,在大多数情况下 - 忽略返回值(因此我们可以讨论了几十年,但没有人会注意到!)

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)

在这种情况下,在做出决定之前,先问这些问题: - 我是否有兴趣与我的来电者就我现有的情况进行交流?(如果我总是返回 0 ......所有事情背后都没有任何线索) - 我的来电者是否有关于此通信的约定?(请注意,单个值不是约定:不允许任何信息表示)

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).

如果这两个答案都不是,那么最好的解决方案可能是根本不编写主要的 return 语句。(并让编译器来决定,关于目标正在工作)。

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).

如果没有约定,0=成功满足大多数情况(并且使用符号可能有问题,如果它们引入了约定)。

If conventions are in place, ensure to use symbolic constants that are coherent with them (and ensure convention coherence, not value coherence, between platforms).

如果约定已到位,请确保使用与其一致的符号常量(并确保平台之间的约定一致性,而不是值一致性)。

回答by Phonon

Once you start writing code that can return a myriad of exit statuses, you start #define'ing all of them. In this case EXIT_SUCCESSmakes sense in context of not being a "magic number". This makes your code more readable because every other exit code will be EXIT_SOMETHING. If you simply write a program that will return when it's done, return 0is valid, and probably even cleaner because it suggests that there's no sophisticated return code structure.

一旦您开始编写可以返回无数退出状态的代码,您就开始编写#define所有这些代码。在这种情况下EXIT_SUCCESS,在不是“幻数”的情况下是有意义的。这使您的代码更具可读性,因为所有其他退出代码都是EXIT_SOMETHING. 如果您只是编写一个完成后将返回的程序,那么它return 0是有效的,甚至可能更清晰,因为它表明没有复杂的返回代码结构。

回答by paulsm4

What you return from a program is just a convention.

你从程序中返回的只是一个约定。

No, I can't think of any circumstances where "EXIT_SUCCESS" wouldn'tbe "0".

不,我想不出任何“EXIT_SUCCESS”不会为“0”的情况。

Personally, I'd recommend "0".

就个人而言,我会推荐“0”。

IMHO...

恕我直言...

回答by Mohammad Moghimi

If you use EXIT_SUCCESS, your code will be more portable.

如果您使用 EXIT_SUCCESS,您的代码将更具可移植性。

http://www.dreamincode.net/forums/topic/57495-return-0-vs-return-exit-success/

http://www.dreamincode.net/forums/topic/57495-return-0-vs-return-exit-success/

回答by Ambidextrous

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.

一些编译器可能会因此产生问题——在 Mac C++ 编译器上,EXIT_SUCCESS 对我来说很好,但在 Linux C++ 编译器上,我必须添加 cstdlib 才能知道 EXIT_SUCCESS 是什么。除此之外,它们是一回事。