- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在看一个计算 3 个数字的平均值的程序,遇到了
#define EXIT_SUCCESS 0
使 return EXIT_SUCCESS;
正常工作(在 include header 下)。使用 #define EXIT_SUCCESS 0
和 return 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_SUCCESS
和 EXIT_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/
我在看一个计算 3 个数字的平均值的程序,遇到了 #define EXIT_SUCCESS 0 使 return EXIT_SUCCESS; 正常工作(在 include header 下)。使用 #
假设我有一个程序,其中有一个类似于 errno 的全局变量(名为 project_error )在其中存储过去发生的确切错误,但我还希望函数返回简单的 OK/NOK 状态以简化错误处理,因此我只检查
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: (C/C++) return EXIT_SUCCESS or 0 from main? 很长一段时间我都在使用
我将在 python 脚本中到处返回 0,但我更喜欢更语义化、更易读的内容。我不喜欢那个神奇的数字。 python 中是否有类似于 C 中如何返回 EXIT_SUCCESS 而不仅仅是 0 的想法?
在 C++ 应用程序的主函数中,我应该何时返回 EXIT_SUCCESS 以及何时返回 EXIT_FAILURE?解压成功是什么意思?举几个例子: 用户忘记了一些必需的命令行参数,我们捕获它,打印帮助
如果您应该从 main 返回 0,并且 EXIT_SUCCESS 是“实现定义的”,它的字面意思是 it is not required to be 0 ,那么如果你不能依赖它是正确的值,它有什么意义
这是一个简单的问题,但我一直看到相互矛盾的答案:C++ 程序的主例程应该返回 0 还是 EXIT_SUCCESS? #include int main(){return EXIT_SUCCESS;}
在您自己的库 API 中使用 EXIT_SUCCESS 和 EXIT_FAILURE 作为函数的返回值是否可以被认为是合理的、好的设计和好的做法? 示例软件是一个低级平台库,在同一产品线的多个产品中用
我正在启动一个进程,想检查它的退出代码是成功还是失败。 Process myProcess = new Process(); myProcess.Start(); myProcess.WaitForE
在 C 程序中,我通常在 exit() 函数中使用 EXIT_SUCCESS 或 EXIT_FAILURE 来提高程序的清晰度和可理解性。 但是在 System.exit() 中我无法使用这些 MAC
ISO 1998 c++ 标准规定在 main 中不显式使用 return 语句等同于使用 return 0。但是如果一个实现有不同的标准“无错误”代码,例如 -1? 为什么不使用标准宏 EXIT_S
这是一个简单的问题,但我一直看到相互矛盾的答案:C++ 程序的主例程应该返回 0 还是 EXIT_SUCCESS? #include int main(){return EXIT_SUCCESS;}
如果我使用 return 0 而不是: 有什么不同吗? system("PAUSE"); return EXIT_SUCCESS 这是脚本: #include using namespace std
我知道 return vs exit() 之间的区别. 我想知道它们与 FreeConsole() 有何不同。 FreeConsole()是一个API函数 (scope: main function)
Python 2.7.6 中是否有 EXIT_SUCCESS 和 EXIT_FAILURE 宏的类似物?如果是,我该如何使用它? 最佳答案 是的,使用 os.EX_OK .(仅限 Unix) 正如文档
我是一名优秀的程序员,十分优秀!