INT的main()VS用C无效的主要() [英] int main() vs void main() in C
问题描述
在C,我知道,INT的main()函数返回一个int,其中无效的主要()不会。除此之外,有它们之间的差?首先是比第二好?
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 first better than second?
推荐答案
绝大多数的时间,的一个INT主要(无效)
或 INT主(INT ARGC,CHAR *的argv [])
是你需要使用的东西。特别是,如果你正在写一个程序,将是任何重大的编译器的个人电脑上运行的编译,拥有整套的C标准库,那么你几乎肯定需要将返回一个 INT
从主
。
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
.
(我也尽量避免使用空的参数列表,请参阅为什么不我们主要使用(无效)? )
(I would also avoid using an empty argument list, see "why dont 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
例如,HITECH PICC32编译器Microchip的PIC32MX系列微控制器的要求无效的主要(无效)
署名 - 因为说真的,这是怎么回事永远看到的返回值?在另一方面,Microchip在同一处理器(基于GCC)编译器正式进入与通常的 INT主要(无效)
。
For example, the HITECH PICC32 compiler for Microchip's PIC32MX series of microcontrollers requires a void main(void)
signature — because really, what's going to ever see the return value? On the other hand, Microchip's official compiler for the same processor (based on GCC) goes with the usual int main(void)
.
个人而言,我会避免他们,即使他们的是的允许的。
Personally I would avoid them even if they are allowed.
参见下面的为什么不我们主要使用(无效)?中的注释,这方面的一些有趣的讨论。
See the comments below "why dont we use (void) in main?" for some interesting discussion on this.
这篇关于INT的main()VS用C无效的主要()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!