C 中的 int main() 与 void main() [英] int main() vs void main() in C

查看:43
本文介绍了C 中的 int main() 与 void main()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,我知道 int main() 返回 intvoid main() 不返回.除此之外,它们之间还有区别吗?第一个比第二个好吗?

In C, I know that int main() returns an int where void main() does not. Other than that, is there a difference between them? Is the first better than the second?

推荐答案

绝大多数时候,int main(void)int main(int argc, char*argv[]) 是你需要使用的.特别是,如果您正在编写一个程序,该程序将由任何主要编译器编译以在个人计算机上运行,​​并使用完整的 C 标准库集,那么您几乎肯定需要返回一个 int 来自 main.

The overwhelming majority of the time, one of int main(void) or int main(int argc, char* argv[]) is what you need to use. In particular, if you're writing a program that's going to be compiled by any major compiler for running on a personal computer, with the full set of the C standard libraries, then you almost certainly need to be returning an int from main.

(我也会避免使用空参数列表,请参阅为什么我们不在 main 中使用 (void)?a>")

(I would also avoid using an empty argument list, see "Why don't we use (void) in main?")

C99 标准确实允许其他实现定义的签名,如果您已阅读编译器手册并且它说可以,则可以使用这些签名.

The C99 standard does allow for other implementation-defined signatures, and you can use these if you've read the manual for your compiler and it says you can.

(5.1.2.2.1) 应使用 int 返回类型定义且不带参数...或带两个参数...或以其他实现定义的方式

(5.1.2.2.1) It shall be defined with a return type of int and with no parameters ... or with two parameters ... or in some other implementation-defined manner

就我个人而言,即使它们被允许(如果可能),我也会避免它们,因为如果您需要移植到另一个系统,则需要担心另一件事.

Personally I would avoid them even if they are allowed (if possible), because it's one more thing to worry about if you ever need to port to another system.

请参阅下面的评论为什么我们不在 main 中使用 (void)?",以了解一些有趣的讨论这个.

See the comments below "Why don't we use (void) in main?" for some interesting discussion on this.

这篇关于C 中的 int main() 与 void main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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