为什么C和C++中main函数的类型留给用户定义?

Why is the type of the main function in C and c++ left to the user to define?(为什么C和C++中main函数的类型留给用户定义?)
本文介绍了为什么C和C++中main函数的类型留给用户定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 main() 是用户定义的函数?

Why is main() a user defined function ?

我什么时候使用 void main()int main()?

When will I use void main() and int main()?

推荐答案

EDIT 这个答案并不完整,因为它没有真正解决奇怪的句子或其他实现定义的方式".我现在写了一个更完整的答案它还解决了 C90、C11 和 C++.编辑结束

EDIT This answer is not as complete as it could be since it doesn't really address the strange sentence "or otherwise in some implementation-defined manner". I have now written a more complete answer which also addresses C90, C11 and C++. END OF EDIT

以下是 C 标准的说明(ISO C 9899:1999):

Here is what the C standard says (ISO C 9899:1999):

5.1.2.1 独立环境

5.1.2.1 Freestanding environment

在一个独立的环境中(在其中 C程序执行可能发生没有任何经营利益系统),名称和类型程序启动时调用的函数是实现定义./../效果程序终止在一个独立的环境是实现定义.

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. / .. / The effect of program termination in a freestanding environment is implementation-defined.

5.1.2.2 托管环境

5.1.2.2 Hosted environment

不需要提供托管环境,但是应符合以下规格(如果有).

A hosted environment need not be provided, but shall conform to the following specifications if present.

5.1.2.2.1 程序启动

5.1.2.2.1 Program startup

程序启动时调用的函数名为主要的.实现声明没有这个函数的原型.它应用 int 返回类型定义并且没有参数:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) {/* ... *

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)