5

可能重复:
main() 在 C/C++ 中应该返回什么?

大约一个小时前才开始编写 C 代码,经过几个月的基本 java 编码后,在编译基本的 hello world 程序时遇到了问题。

这是我的代码:

#include < stdio.h>

void main()
{
    printf("\nHello World\n");
}

这就是我尝试编译时得到的结果:

 Hello.c: In function ‘main’:
 Hello.c:13: warning: return type of ‘main’ is not ‘int’

任何帮助将不胜感激,谢谢!

4

4 回答 4

5

The standard signatures for main are either

int main(void)

or

int main(int argc, char **argv)

Your compiler is simply enforcing the standard.

Note that an implementation may support void main(), but it must be explicitly documented, otherwise the behavior is undefined. Like dandan78 says, a large number of books and online references get this wrong.

于 2013-01-31T21:57:01.713 回答
4

它应该是

int main() {}

那么return 0如果程序正确终止或任何其他数字(如果出现错误),您应该这样做。这是 Unix 约定,因此脚本可以检查程序是否正确终止或发生错误。

于 2013-01-31T21:51:26.900 回答
2

c 中的 main-function 必须返回一个 int:

#include < stdio.h>

int main()
{
  printf("\nHello World\n");
  return 0;
}
于 2013-01-31T21:51:50.277 回答
-2

无论您选择哪个原型main(),它的返回值都不能是void. 它必须是int。许多书籍和教程都犯了这个错误,一些编译器倾向于抱怨,而另一些则没有。

于 2013-01-31T21:51:39.983 回答