为什么我们需要在 C++ 中使用 `int main` 而不是 `void main`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/449851/
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
Why do we need to use `int main` and not `void main` in C++?
提问by kasperasky
Why do we need to use int main
and not void main
in C++?
为什么我们需要使用int main
而不是void main
在 C++ 中?
回答by Greg Hewgill
The short answer, is because the C++ standard requires main()
to return int
.
简短的回答是因为 C++ 标准要求main()
返回int
.
As you probably know, the return value from the main()
function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished. Returning a value from main()
provides one way for the programmer to specify this value.
您可能知道,main()
运行时库将函数的返回值用作进程的退出代码。Unix 和 Win32 都支持进程完成后从进程返回的(小)整数的概念。返回值为main()
程序员提供了一种指定该值的方法。
回答by abelenky
Most Operating Systems report back to the user, or the calling process, if an application was successful or not. This is especially useful in scripting, where the script can conditionally branch (if-then) on the results of a program. Something along the lines of:
大多数操作系统会向用户或调用进程报告应用程序是否成功。这在脚本编写中特别有用,脚本可以根据程序的结果有条件地分支(if-then)。类似的东西:
// pseudo-code
screenscrape http://mydatasource.com > results.txt
if errorlevel == 0 then
processfile results.txt
else
echo Screen Scraping Failed!
end if
This result status is done via the return value of main.
这个结果状态是通过 main 的返回值完成的。
While some compilers allow for void main, for the sake of consistency and simplicity, the ANSI standard requires one single prototype of main:
虽然一些编译器允许 void main,但为了一致性和简单性,ANSI 标准需要一个单一的 main 原型:
int main(int argc, char *argv[]);
Because in C, arguments are cleaned up by the caller, the author of main can neglect to declare or process the arguments argc & argv. However, if the setup-routines that call main expect an int return value, and instead don't find one, behavior can undefined.
因为在 C 中,参数由调用者清理,main 的作者可以忽略声明或处理参数 argc 和 argv。但是,如果调用 main 的设置例程期望一个 int 返回值,而没有找到,则行为可能未定义。
Short answer:
简短的回答:
- The return value of main is useful for scripting.
- The setup and cleanup routines that invoke main need a consistent interface to use.
- main 的返回值对于编写脚本很有用。
- 调用 main 的设置和清理例程需要使用一致的接口。
回答by Code Warrior
Main reason for changing
改变的主要原因
void main() { }
无效主(){}
to
到
int main() { }
int main() { }
in later releases was to notify erroroccurred in program during execution to operating systemon which it running
在以后的版本中,将程序在执行过程中发生的错误通知给运行它的操作系统
return 0;
identify program successfully executed if any number rather then 0 returned that means some error occurred who's error codeis which returned by main. if you are running on codeblock IDEsee in build log if main return 0 it normally display
如果返回任何数字而不是 0,则识别程序成功执行,这意味着发生了一些错误,谁的错误代码是由 main 返回的。如果您在代码块 IDE上运行,请在构建日志中查看如果 main 返回 0 它通常会显示
Process terminated with status 0
else it display status code in red which means an error occurred
否则它以红色显示状态代码,这意味着发生了错误
回答by BBetances
From Wikipedia:
来自维基百科:
The value returned from the main function becomes the exit status of the process, though the C standard only ascribes specific meaning to two values:
EXIT_SUCCESS
(traditionally zero) andEXIT_FAILURE
. The meaning of other possible return values is implementation-defined.
从 main 函数返回的值成为进程的退出状态,尽管 C 标准只将特定含义归因于两个值:(
EXIT_SUCCESS
传统上为零)和EXIT_FAILURE
。其他可能的返回值的含义是实现定义的。
回答by Rahul
Perhaps because it makes sense to cleanly exit with a status code from the main()
method. In Java, we have to emulate this using System.exit()
which is not all that graceful.
也许是因为从main()
方法中使用状态代码干净地退出是有意义的。在 Java 中,我们必须模拟这种使用System.exit()
,这并不是那么优雅。
回答by user2786956
When we execute our program to check it runs successfully or not. So when it returns 0 that means it's true & ran successfully, if it returns 1 then it's not run successfully & this int
value tells the OS if the program ran successfully or not
当我们执行我们的程序来检查它是否运行成功。因此,当它返回 0 时,表示它为真并成功运行,如果返回 1 则表示未成功运行,并且此int
值告诉操作系统程序是否成功运行
回答by Keltia
As in C, because the process will give the OS an exit code.
就像在 C 中一样,因为该进程会给操作系统一个退出代码。
You can either use
您可以使用
int main (int argc, char ** argv)
{
return (0);
}
or
或者
int main (int argc, char ** argv)
{
exit (0);
}
This is at least in C89 IIRC.
这至少在 C89 IIRC 中是这样。
回答by Toon Krijthe
Because int is the returncode a program can return to the OS.
因为 int 是程序可以返回到操作系统的返回码。
You can query this value to check if operation has been succesfull.
您可以查询此值以检查操作是否成功。
This was extremely helpfull when using commandline scripts.
这在使用命令行脚本时非常有用。