gpt4 book ai didi

c++ - 为什么 int main() {} 编译?

转载 作者:IT老高 更新时间:2023-10-28 12:32:44 33 4
gpt4 key购买 nike

(我用的是Visual C++ 2008)我一直听说main()是需要返回一个整数,但是这里我没有输入return 0; 并且编译时出现 0 个错误和 0 个警告!在调试窗口中,它说程序已经以代码 0 退出。如果这个函数被命名为 main() 以外的任何名称,编译器会提示说 'blah' 必须返回一个值。粘贴 return; 也会导致错误出现。但是完全忽略它,它编译得很好。

#include <iostream>
using namespace std;

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

这可能是 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;

试图找到 C++ 标准的在线拷贝,以便引用这段 I found a blog post that quotes all the right bits better than I could.

关于c++ - 为什么 int main() {} 编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22239/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com