gpt4 book ai didi

c++ - #define EXIT_SUCCESS 0

转载 作者:太空狗 更新时间:2023-10-29 23:40:16 25 4
gpt4 key购买 nike

我在看一个计算 3 个数字的平均值的程序,遇到了

#define EXIT_SUCCESS 0 

使 return EXIT_SUCCESS; 正常工作(在 include header 下)。使用 #define EXIT_SUCCESS 0return EXIT_SUCCESS; 的目的是什么,还有其他替代方法吗?是不是像return 0;?感谢您的宝贵时间。

这是我正在查看的程序的代码:

#include <iostream>
#define EXIT_SUCCESS 0
using namespace std;

int main()
{
// prototypes:

float add_and_div(float, float, float);

// variables:

float x, y, z;
float result;

cout << "Enter three floats: ";
cin >> x >> y >> z;

// continue the program here

result = add_and_div( x, y, z );
cout<< "Average is \n" << result;

return EXIT_SUCCESS;
}

// function definition:

float add_and_div(float n1, float n2, float n3)
{

// continue the program here
return ( n1 + n2 + n3 ) / 3;
}

最佳答案

EXIT_SUCCESSEXIT_FAILURE 扩展为整数表达式,指示程序执行成功或不成功。它们是 C 标准的一部分,可以在 stdlib.h 中找到。或者更确切地说 cstdlib .

在您的情况下,它将扩展为 0,因此 return EXIT_SUCCESS return 0 相同。但是,该宏已在标准库中定义,因此您应该使用该宏而不是定义您自己的宏。请注意,在 exit 中返回或使用 EXIT_SUCCESS 的行为与在这些情况下使用 0 的行为相同,所以这取决于你想要什么使用。

关于c++ - #define EXIT_SUCCESS 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22586250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com