admin管理员组

文章数量:1618690

C/C++ 中的 EXIT_SUCCESS and EXIT_FAILURE

  • 1. C 中的 `EXIT_SUCCESS` and `EXIT_FAILURE`
    • 1.1. Example (`EXIT_SUCCESS` and `EXIT_FAILURE`)
  • 2. C++ 中的 `EXIT_SUCCESS` and `EXIT_FAILURE`
    • 2.1. `EXIT_SUCCESS` - Success termination code (macro)
    • 2.2. `EXIT_FAILURE` - Failure termination code (macro)
  • 3. `stdlib.h`
  • References

1. C 中的 EXIT_SUCCESS and EXIT_FAILURE

https://en.cppreference/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/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/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/
[2] EXIT_SUCCESS, EXIT_FAILURE, https://en.cppreference/w/cpp/utility/program/EXIT_status
[3] EXIT_SUCCESS, EXIT_FAILURE, https://en.cppreference/w/c/program/EXIT_status
[4] EXIT_SUCCESS, http://www.cplusplus/reference/cstdlib/EXIT_SUCCESS/
[5] EXIT_FAILURE, http://www.cplusplus/reference/cstdlib/EXIT_FAILURE/

本文标签: EXITSUCCESSEXITFAILURE