main() 在 C 和 C++ 中应该返回什么? [英] What should main() return in C and C++?
问题描述
在 C 和 C++ 中定义 main()
函数的正确(最有效)方法是什么 - int main()
或 void main()
— 为什么?那么论据呢?如果 int main()
那么 return 1
还是 return 0
?
What is the correct (most efficient) way to define the main()
function in C and C++ — int main()
or void main()
— and why? And how about the arguments?
If int main()
then return 1
or return 0
?
这个问题有很多重复,包括:
- 什么是 C 的
的有效签名main()
函数? main()
函数的返回类型void main()
和int main()
?main()
在 C++ 中的签名莉>- 什么是
main()
? — 对于 C++,确实是一个很好的答案. - C 中
main()
函数的样式一个> main()
方法的返回类型Cint main()
vsvoid main()
in C
- What are the valid signatures for C's
main()
function? - The return type of
main()
function - Difference between
void main()
andint main()
? main()
's signature in C++- What is the proper declaration of
main()
? — For C++, with a very good answer indeed. - Styles of
main()
functions in C - Return type of
main()
method in C int main()
vsvoid main()
in C
相关:
- C++ —
int main(int argc, char **argv)
- C++ —
int main(int argc, char *argv[])
- 是
char *envp[]
作为main()
可移植的第三个参数? - 必须是
int main()
函数在所有编译器中都返回一个值? - 为什么C和C++中
main()
函数的类型留给用户定义? - 为什么要编译
int main(){}
?一个> - C++ 中
main()
的法律定义14?
- C++ —
int main(int argc, char **argv)
- C++ —
int main(int argc, char *argv[])
- Is
char *envp[]
as a third argument tomain()
portable? - Must the
int main()
function return a value in all compilers? - Why is the type of the
main()
function in C and C++ left to the user to define? - Why does
int main(){}
compile? - Legal definitions of
main()
in C++14?
推荐答案
main
的返回值指示程序如何退出.正常退出由 main
的 0 返回值表示.非零返回表示异常退出,但没有关于如何解释非零代码的标准.正如其他人所指出的,void main()
是 C++ 标准禁止的,不应使用.有效的 C++ main
签名是:
The return value for main
indicates how the program exited. Normal exit is represented by a 0 return value from main
. Abnormal exit is signaled by a non-zero return, but there is no standard for how non-zero codes are interpreted. As noted by others, void main()
is prohibited by the C++ standard and should not be used. The valid C++ main
signatures are:
int main()
和
int main(int argc, char* argv[])
相当于
int main(int argc, char** argv)
还值得注意的是,在 C++ 中,int main()
可以不带返回语句,此时它默认返回 0.这对于 C99 程序也是如此.return 0;
是否应该省略还有待商榷.有效的 C 程序主签名的范围要大得多.
It is also worth noting that in C++, int main()
can be left without a return-statement, at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0;
should be omitted or not is open to debate. The range of valid C program main signatures is much greater.
效率不是 main
函数的问题.根据C++标准,它只能进入和离开一次(标记程序的开始和终止).对于 C,允许重新输入 main()
,但应避免.
Efficiency is not an issue with the main
function. It can only be entered and left once (marking the program's start and termination) according to the C++ standard. For C, re-entering main()
is allowed, but should be avoided.
这篇关于main() 在 C 和 C++ 中应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!