没有返回类型定义main()函数给出警告 [英] main() function defined without return type gives warning

查看:140
本文介绍了没有返回类型定义main()函数给出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的程序:

main()
{ 
    printf("hello world\n");
}

为什么它给这个警告呢?
函数返回值

Why does it give this warning? "function should return a value"

但是,当我变的main()到无效的主要()中,有没有出现这样的警告。
为什么会这样?

But when I change "main()" to "void main()", there appears no such warnings. Why is that so?

推荐答案

有,你应该注意的几件事情:

There are few things which you should take note of :


  1. INT 的main()函数的返回类型。这意味着什么样的价值的main() CAN
    返回是一个整数。

  2. 的main()是由C90编译​​器而不是由C99编译器,这意味着它不是C99标准的一部分了容忍的,所以不这样做。

  3. 无效的主要()是不是一个标准的形式,一些编译器允许这样做,但没有标准曾经将其列为一个选项。因此,
    编译器不必接受这种形式,和几个没有。 再次,坚持标准形式,
    如果你从一个编译器移动程序到另一个你会不会遇到问题。

  4. 和最后一件事,而不是编写主要是这样的:

  1. The int is the main() function's return type. That means that the kind of value main() can return is an integer.
  2. 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.
  3. 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.
  4. And one last thing , instead of writing main like this :

INT的main() // 在这里你正在沉默参数传递给主,这意味着它可能会或可能不会接受参数

这样写:

int main(void)// this specifies there are no arguments taken by main

您可能想看看在 C99标准 了解更多详情

这篇关于没有返回类型定义main()函数给出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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