本文介绍了没有返回类型定义的 main() 函数给出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的程序:
main()
{
printf("hello world
");
}
编译时收到此警告:
function should return a value
将main()
改为void main()
时,警告消失.
为什么会这样?
推荐答案
有几点需要注意:
int
是main()
函数的返回类型.这意味着main()
类型的值可以return 是一个整数.main()
被 C90 编译器允许,但 C99 编译器不允许,这意味着它不再是 C99 标准的一部分,所以不要这样做.void main()
不是标准形式,一些编译器允许这样做,但没有一个标准将其列为选项.所以,编译器不必接受这种形式,有些则不需要.再次,坚持标准形式,如果您将程序从一个编译器移动到另一个编译器,您也不会遇到问题.最后一件事,而不是像这样写 main :
- The
int
is themain()
function's return type. That means that the kind of valuemain()
canreturn is an integer. main( )
was tolerated by the C90 compilers but not by C99 compilers which means its not a part of C99 standard anymore , so don't do this.void main()
is not a standard form ,some compilers allow this, but none of the standards have ever listed it as an option. Therefore,compilers don't have to accept this form, and several don't. Again, stick to the standard form,and you won't run into problems if you move a program from one compiler to another.And one last thing , instead of writing main like this :
int main()
//在这里你对向 main 传递参数保持沉默,这意味着它可能会或可能不会接受参数
这样写:
int main(void)// this specifies there are no arguments taken by main
您可能想看看 C99 标准 了解更多详情.
这篇关于没有返回类型定义的 main() 函数给出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!