Why default return value of main is 0 and not EXIT_SUCCESS?(为什么 main 的默认返回值为 0 而不是 EXIT_SUCCESS?)
问题描述
ISO 1998 c++ 标准规定在 main 中不显式使用 return 语句等同于使用 return 0.但是如果一个实现有不同的标准无错误"代码,例如 -1?
The ISO 1998 c++ standard specifies that not explicitly using a return statement in the main is equivalent to use return 0.
But what if an implementation has a different standard "no error" code, for example -1?
为什么不使用标准宏 EXIT_SUCCESS 将被 0 或 -1 或任何其他值替换,具体取决于实现?
Why not use the standard macro EXIT_SUCCESS that would be replaced either by 0 or -1 or any other value depending on the implementation?
C++ 似乎强加了程序的语义,这不是只应描述程序行为方式的语言的角色.此外,错误"返回值的情况有所不同:只有 EXIT_FAILURE 是标准的错误"终止标志,没有明确的值,例如1".
C++ seems to force the semantic of the program, which is not the role of a language which should only describe how the program behaves. Moreover the situation is different for the "error" return value: only EXIT_FAILURE is a standard "error" termination flag, with no explicit value, like "1" for example.
这些选择的原因是什么?
What are the reasons of these choices?
推荐答案
从 main() 返回零与您所要求的基本相同.从 main() 返回零不必将零返回到主机环境.
Returning zero from main() does essentially the same as what you're asking. Returning zero from main() does not have to return zero to the host environment.
来自 C90/C99/C++98 标准文档:
From the C90/C99/C++98 standard document:
如果status的值为0或EXIT_SUCCESS,则返回成功终止的实现定义形式.
If the value of status is zero or
EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
这篇关于为什么 main 的默认返回值为 0 而不是 EXIT_SUCCESS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 main 的默认返回值为 0 而不是 EXIT_SUCCESS?
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
