#define EXIT_SUCCESS 0

Syn*_*ror 2 c++ function function-prototypes

我正在看一个计算3个数字平均值的程序

#define EXIT_SUCCESS 0 
Run Code Online (Sandbox Code Playgroud)

使return EXIT_SUCCESS;工作没有错误(在包含标题下).什么是使用的目的#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;
}
Run Code Online (Sandbox Code Playgroud)

Zet*_*eta 5

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

在你的情况下,它将扩展到0,所以return EXIT_SUCCESS 相同的return 0.但是,宏已经在标准库中定义,因此您应该使用该宏而不是定义自己的宏.请注意,返回或使用EXIT_SUCCESSin exit将具有与0在这些事件中使用相同的行为,因此由您决定使用.