C语言 Hello.c:在函数“main”中:Hello.c:13:警告:“main”的返回类型不是“int”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14635786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hello.c: In function ‘main’: Hello.c:13: warning: return type of ‘main’ is not ‘int’?
提问by Joe Perkins
Possible Duplicate:
What should main() return in C/C++?
可能的重复:
main() 在 C/C++ 中应该返回什么?
Just started coding C about an hour ago, after a few months of basic java coding, and am encountering a problem compiling the basic hello world program.
大约一个小时前刚刚开始编码 C,经过几个月的基本 Java 编码,我在编译基本的 hello world 程序时遇到了问题。
Here is my code:
这是我的代码:
#include < stdio.h>
void main()
{
printf("\nHello World\n");
}
and this is what i get back when i try to compile:
这就是我尝试编译时得到的结果:
Hello.c: In function ‘main':
Hello.c:13: warning: return type of ‘main' is not ‘int'
any help would be much apprecated, thanks!
任何帮助将不胜感激,谢谢!
回答by John Bode
The standard signatures for mainare either
的标准签名main是
int main(void)
or
或者
int main(int argc, char **argv)
Your compiler is simply enforcing the standard.
您的编译器只是在执行标准。
Note that an implementation maysupport void main(), but it must be explicitly documented, otherwise the behavior is undefined. Like dandan78 says, a largenumber of books and online references get this wrong.
请注意,实现可能支持void main(),但必须明确记录,否则行为未定义。就像 dandan78 说的,大量的书籍和在线参考资料都弄错了。
回答by LtWorf
it should be
它应该是
int main() {}
then you should return 0if the program is terminating correctly or any other number if there was an error. That's an Unix convention, so scripts can check if the program was terminated correctly or an error occurred.
那么您应该return 0在程序正确终止或任何其他数字(如果有错误)。这是 Unix 约定,因此脚本可以检查程序是否正确终止或发生错误。
回答by pbhd
main-function in c has to return an int:
c 中的主函数必须返回一个整数:
#include < stdio.h>
int main()
{
printf("\nHello World\n");
return 0;
}
回答by dandan78
Regardless of which prototype you choose for main(), it's return value cannot be void. It has to be int. Many books and tutorials get this wrong and some compilers tend to complain while others do not.
无论您选择哪种原型main(),它的返回值都不能是void. 它必须是int。许多书籍和教程都弄错了,有些编译器倾向于抱怨,而另一些则没有。

