C语言 EXIT_FAILURE 与退出(1)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13667364/
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-09-02 04:38:43  来源:igfitidea点击:

EXIT_FAILURE vs exit(1)?

cexit

提问by temporary_user_name

What's the difference? Which is preferred, or when should I use each one respectively?

有什么不同?哪个是首选,或者我应该什么时候分别使用每个?

回答by Alok Save

exit(1)(usually) indicates unsuccessful termination. However, its usage is non-portable. For example, on OpenVMS, exit(1)actually indicates success.

exit(1)(通常)表示不成功的终止。然而,它的使用是不可移植的。例如,在OpenVMS 上exit(1)实际上表示成功。

Only EXIT_FAILUREis the standard value for returning unsuccessful termination, but 1is used for the same in many implementations.

OnlyEXIT_FAILURE是返回不成功终止的标准值,但1在许多实现中用于相同的值。



So to summarize:
If you want to write perfectly portable code use,

所以总结一下:
如果你想编写完美可移植的代码使用,

EXIT_FAILUREfor failure case. While,
You can use either exit(0)or EXIT_SUCCESSfor success case.

EXIT_FAILURE对于失败的情况。同时,
您可以使用exit(0)EXIT_SUCCESS用于成功案例。

Note that, EXIT_SUCCESSor 0are both same.

请注意,EXIT_SUCCESS0两者都相同。



Reference:

参考:

C99 Standard: 7.20.4.3 The exitfunction
Para 5

C99 标准:7.20.4.3exit功能
第 5 段

Finally, control is returned to the host environment. If the value of status is zeroor EXIT_SUCCESS, an implementation-de?ned form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-de?ned form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-de?ned.

最后,控制权返回给宿主环境。如果状态的值为zeroEXIT_SUCCESS,则返回状态成功终止的实现定义形式。如果状态的值为EXIT_FAILURE,则返回状态不成功终止的实现定义形式。否则返回的状态是实现定义的。

回答by Jerry Coffin

For truly portable code, EXIT_FAILUREis preferred. The C standard only defines meaning for three values: EXIT_FAILURE, 0, and EXIT_SUCCESS(with 0and EXIT_SUCCESSessentially synonymous).

对于真正可移植的代码,EXIT_FAILURE是首选。C标准仅定义意为三个值:EXIT_FAILURE0,及EXIT_SUCCESS(具有0EXIT_SUCCESS基本上同义)。

From a practical viewpoint, mosttypical systems accept other values as well. If memory serves, Linux will let you return any 8-bit value, and Windows 16-bit values. Unless you honestly might care about porting to an IBM mainframe, VMS, etc., chances are you don't care about most of the systems that won't support at least 8-bit return values.

从实用的角度来看,大多数典型系统也接受其他值。如果没记错的话,Linux 将允许您返回任何 8 位值和 Windows 16 位值。除非您真的关心移植到 IBM 大型机、VMS 等,否则您很可能不关心大多数不支持至少 8 位返回值的系统。

回答by Ed Heal

Use EXIT_FAILURE. It is a constant that is used throughout the OS. Its value could be something else than 1 and also it is more descriptive in the code.

使用EXIT_FAILURE. 它是在整个操作系统中使用的常量。它的值可能不是 1,而且它在代码中更具描述性。

回答by Ravindra Bagale

There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure. Programs that perform comparison use a different convention: they use status 1 to indicate a mismatch, and status 2 to indicate an inability to compare. Your program should follow an existing convention if an existing convention makes sense for it.

对于某些程序应该返回什么样的状态值,有一些约定。最常见的约定是 0 表示成功,1 表示失败。执行比较的程序使用不同的约定:它们使用状态 1 表示不匹配,使用状态 2 表示无法比较。如果现有约定对其有意义,您的程序应该遵循现有约定。

Some non-POSIX systems use different conventions for exit status values. For greater portability, you can use the macros EXIT_SUCCESSand EXIT_FAILUREfor the conventional status value for success and failure, respectively. They are declared in the file stdlib.h.

一些非 POSIX 系统对退出状态值使用不同的约定。For greater portability,您可以分别使用宏EXIT_SUCCESSEXIT_FAILURE用于成功和失败的常规状态值。它们在文件 stdlib.h 中声明。