C语言 void main 和 int main 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23152247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between void main and int main
提问by Robin
I have seen many programs that has either int main()or void main(). I know both are used as the starting point of a program. I am just confused of when to use one and avoid another. I would really appreciate if you could explain it to me.
我见过许多程序具有int main()或void main(). 我知道两者都用作程序的起点。我只是对何时使用一种而避免另一种感到困惑。如果您能向我解释一下,我将不胜感激。
回答by DrakaSAN
In the current ISO standard, it doesn't change anything, use what the other developers you work with expect. (credits to Lundin to remind me of that)
在当前的 ISO 标准中,它不会改变任何东西,使用与您一起工作的其他开发人员所期望的。(感谢 Lundin 提醒我这一点)
For ANSI C, int mainmeans you will end your program with return 0;(or other value, 0 is the standard for "everything's fine").
对于 ANSI C,int main意味着您将以return 0;(或其他值,0 是“一切正常”的标准)结束您的程序。
void mainwill allow you to skip that line, and have some other effect, basically, depending on compiler, you may not be able to access argc and argv since the main take 0 arguments.
void main将允许您跳过该行,并产生一些其他效果,基本上,取决于编译器,您可能无法访问 argc 和 argv,因为主要采用 0 参数。
Although it doesn't do a lot of bad, it's better in my opinion to use int main, so you don't have to worry about the side effect. It's also the norm in ANSI C.
虽然它不会做很多坏事,但我认为使用它更好int main,所以你不必担心副作用。这也是 ANSI C 中的规范。
回答by user2105505
int main()simply returns a result code to the OS after the execution of your code. For example, returning 0usually means a successful run, and returning anything else means there was probably an error. Other than debugging and maybe bug reporting, there really isn't much use for returning an intfrom the main function. And of course, voidmeans that you don't return anything. Good luck!
int main()只需在执行代码后将结果代码返回给操作系统。例如,返回0通常意味着运行成功,而返回任何其他内容则意味着可能存在错误。除了调试和可能的错误报告之外,int从 main 函数返回 an 确实没有太大用处。当然,void意味着你不返回任何东西。祝你好运!

