如果编译得很好,`void main`怎么会无效?

Min*_*mai 1 c c++ program-entry-point

我找到了int main定义,但没有void main介绍c ++编程语言.我试着阅读所有用c ++编程语言介绍的文章.

Che*_*Alf 6

void main 从来没有在C或C++中有效.

C++11§3.6.1/ 2:

"实现不应预定义该main功能.此功能不应过载.它应具有类型的返回类型int,否则其类型是实现定义的.所有实现都应允许以下两个定义main:

int main() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

int main(int argc, char* argv[]) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

...

C++11§3.6.1/ 5:

"return语句中main具有离开的效果main功能(破坏用自动存储持续时间的任何对象),并调用std::exit与返回值作为参数.如果控件在main没有遇到return语句的情况下到达结尾,则效果就是执行

return 0;
Run Code Online (Sandbox Code Playgroud)

值0是表示过程成功的一个值.值EXIT_SUCCESSfrom <stdlib.h>可以是0或其他值.该值EXIT_FAILURE表示进程失败.

  • @MinSomai如果你真的想学C++那么我建议你新老师/学校.使用turbo C++学习C++会对你造成伤害. (6认同)
  • Turbo C++也将接受标准的`int main`.指出你的老师这个答案.我将添加相关的标准(所以他可以学习:)). (2认同)
  • @MinSomai Turbo C++?这个20岁的编译器?这早于任何C++标准AFAIK. (2认同)

dbu*_*ush 5

使用void main不是标准的,尽管有一些关于为什么某些编译器允许它的历史。

以下是 2008 年在programingforums.org上的一篇文章:

1971 年,Denis Ritchie(与 Ken Thomson 一起开发 Unix)通过添加对字符类型的支持产生了“新 B”,并修改了早期的 B 编译器以输出机器代码。1972年,“new B”更名为C。 1973年左右增加了一个预处理器,团队中的另一个程序员制作了“便携式I/O包”,后来更名为C“标准I/O例程” . 因此 C 的第一个版本诞生了:它只支持字符、整数和指针类型。没有 void 关键字。

Denis Ritchie and Brian Kernighan worked together to enhance C. Later versions of C introduced more of the standard library, such as malloc() - which originally returned pointer to char, as there was no void pointer. Hence it was always necessary in early versions of C to cast the return from malloc(). Support for floating point was also added. This became what is known as Kernighan and Ritchie (K&R) C.

In the early 1980s, a decision was made to ratify C as a standard, leading to the development of the first ANSI Standard in 1989 (then ratified as an ISO standard in 1990). In the committee process leading to the standard, a number of changes were made: in particular the void keyword was introduced, the form of function prototypes was changed.

During the standardisation process, several commercial compilers were developed. Some of these supported void main() as a work-around for compiler diagnostics about falling off the end of main(). They lobbied unsuccessfully for this to be supported by the C standard, but that was not accepted as other vendors considered it added no significant or useful new functionality. Later, in the early 1990s, when "standard compliance" became a marketing tool, they unleashed lawyers on the standard and found the wording loop-hole that - they then claimed - allows void main() to be considered as standard.

During the C standardisation process, Bjarne Stroustrup started work on the development of C++, and published the ARM (Annotated Reference Manual) with Margaret Ellis in 1990. Since that happened in parallel with the minor flurry associated with void main(), that feature was never part of C++. The ARM was the basis for development of the C++ standard, which was finally ratified by ANSI in 1998 and ISO in 1999.

During development of the 1999 C standard, there was some discussion about void main(), but it never gained traction - the push in favour was political, and overall consensus was apparently that it offered little technical benefit. Hence the 1999 C standard explicitly disallows it.