为什么int main(){}编译? [英] Why does int main() {} compile?

查看:209
本文介绍了为什么int main(){}编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我使用Visual C ++ 2008)我一直听说main()是需要返回一个整数,但在这里我没有放在 return 0; 和它编译时带有0个错误和0个警告!在调试窗口中,它说程序已退出与代码0.如果这个函数命名任何东西除了main(),编译器抱怨说blah必须返回一个值。粘贴返回; 也会导致错误出现。

  #include< iostream> 
using namespace std;

int main()
{
cout< 嘿看看我应该返回一个int,但我不会!\\\
;
}

这可能是VC ++中的错误吗?

解决方案


3.6.1主函数



p>

2实现不应预先定义 main 函数。此功能不应重载。它应该具有 int 类型的返回类型,否则其类型是实现定义的。所有实现应允许main的以下定义:

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

  int main(int argc,char * argv []){
/ * ... * /
}

....并继续添加...



5 A return main 中的c $ c>语句具有离开main函数(销毁具有自动存储持续时间的任何对象)并调用 exit 以返回值作为参数。 如果控制在未遇到return语句的情况下达到 main 的结尾,效果是执行return 0 ;


尝试查找C ++标准的在线副本,因此我可以引用此段落我发现了一篇博客文章,其中引用了所有正确的位。


(I'm using Visual C++ 2008) I've always heard that main() is required to return an integer, but here I didn't put in return 0; and and it compiled with 0 errors and 0 warnings! In the debug window it says the program has exited with code 0. If this function is named anything other than main(), the compiler complains saying 'blah' must return a value. Sticking a return; also causes the error to appear. But leaving it out completely, it compiles just fine.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hey look I'm supposed to return an int but I'm not gonna!\n";
}

Could this be a bug in VC++?

解决方案

3.6.1 Main function

....

2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

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

and

int main(int argc, char* argv[]) {
/* ... */
}

.... and it continues to add ...

5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

attempting to find an online copy of the C++ standard so I could quote this passage I found a blog post that quotes all the right bits better than I could.

这篇关于为什么int main(){}编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆