C/C++ 中的 EXIT_SUCCESS and EXIT_FAILURE

1. C 中的 EXIT_SUCCESS and EXIT_FAILURE

https://en.cppreference.com/w/c/program/EXIT_status

Defined in header <stdlib.h> - 定义于头文件 <stdlib.h>

#define EXIT_SUCCESS /*implementation defined*/
#define EXIT_FAILURE /*implementation defined*/

The EXIT_SUCCESS and EXIT_FAILURE macros expand into integral expressions that can be used as arguments to the std::exit function (and, therefore, as the values to return from the main function), and indicate program execution status.
EXIT_SUCCESSEXIT_FAILURE 展开成整数常量表达式能用做 std::exit 函数的参数 (从而能用做从 main 函数返回的值),并指示程序执行状态。

EXIT_SUCCESS - successful execution of a program (程序的成功执行)
EXIT_FAILURE - unsuccessful execution of a program (程序的不成功执行)

Both EXIT_SUCCESS and the value zero indicate successful program execution status (see std::exit), although it is not required that EXIT_SUCCESS equals zero.
EXIT_SUCCESS 与值 0 均指示成功的程序执行状态 (见 std::exit),尽管不要求 EXIT_SUCCESS 等于零。

1.1. Example (EXIT_SUCCESS and EXIT_FAILURE)

//============================================================================
// Name        : EXIT_SUCCESS, EXIT_FAILURE
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	FILE *fp = fopen("data.txt", "r");
	if (fp == NULL)
	{
		fprintf(stderr, "fopen() failed in file %s at line # %d\n", __FILE__, __LINE__);
		exit(EXIT_FAILURE);
	}

	/* Normal processing continues here. */
	fclose(fp);
	printf("Normal Return\n");

	return EXIT_SUCCESS;
}

Output:

fopen() failed in file ../src/hello_world.cpp at line # 17

2. C++ 中的 EXIT_SUCCESS and EXIT_FAILURE

https://en.cppreference.com/w/cpp/utility/program/EXIT_status

Defined in header <cstdlib> - 定义于头文件 <cstdlib>

#define EXIT_SUCCESS /*implementation defined*/
#define EXIT_FAILURE /*implementation defined*/

The EXIT_SUCCESS and EXIT_FAILURE macros expand into integral expressions that can be used as arguments to the std::exit function (and, therefore, as the values to return from the main function), and indicate program execution status.

EXIT_SUCCESS - successful execution of a program (程序的成功执行)
EXIT_FAILURE - unsuccessful execution of a program (程序的不成功执行)

Both EXIT_SUCCESS and the value zero indicate successful program execution status (see std::exit), although it is not required that EXIT_SUCCESS equals zero.

macro [ˈmækrəʊ]:n. (计算机) 宏指令,宏,微距镜头 adj. 大规模的,宏观的,微距摄影的,巨大的,大量的
termination [ˌtɜːmɪˈneɪʃn]:n. 结束,终止

2.1. EXIT_SUCCESS - Success termination code (macro)

https://cplusplus.com/reference/cstdlib/EXIT_SUCCESS/

This macro expands to a system-dependent integral expression that, when used as the argument for function exit, signifies that the application was successful.
该宏扩展为依赖于系统的整数表达式,当用作函数退出的参数时,表示应用程序已成功。

The opposite meaning can be specified with EXIT_FAILURE.
可以使用 EXIT_FAILURE 指定相反的含义。

2.2. EXIT_FAILURE - Failure termination code (macro)

This macro expands to a system-dependent integral expression that, when used as the argument for function exit, signifies that the application failed.
该宏扩展为依赖于系统的整数表达式,当用作函数出口的参数时,表示应用程序失败。

The opposite meaning can be specified with EXIT_SUCCESS.
可以使用 EXIT_SUCCESS 指定相反的含义。

3. stdlib.h

/* We define these the same for all machines.
   Changes from this to the outside world should be done in `_exit'. */
#define	EXIT_FAILURE	1	/* Failing exit status.  */
#define	EXIT_SUCCESS	0	/* Successful exit status.  */

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] EXIT_SUCCESS, EXIT_FAILURE, https://en.cppreference.com/w/cpp/utility/program/EXIT_status
[3] EXIT_SUCCESS, EXIT_FAILURE, https://en.cppreference.com/w/c/program/EXIT_status
[4] EXIT_SUCCESS, http://www.cplusplus.com/reference/cstdlib/EXIT_SUCCESS/
[5] EXIT_FAILURE, http://www.cplusplus.com/reference/cstdlib/EXIT_FAILURE/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值