又该C和C ++的main()的回报? [英] What should main() return in C and C++?

查看:138
本文介绍了又该C和C ++的main()的回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正确的(最有效)的方式来定义在C和C ++中的的main()功能 - INT的main()无效的主要() - 为什么?
如果 INT的main()然后收益1 返回0

What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? If int main() then return 1 or return 0?

有此问题的众多副本,其中包括:

  • What are the valid signatures for C's main() function?
  • The return type of main() function
  • Difference between void main() and int 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() vs void main() in C

相关阅读:

  • C++ — int main(int argc, char **argv)
  • C++ — int main(int argc, char *argv[])
  • Is char *envp[] as a third argument to main() 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?

推荐答案

返回值主应说明程序是如何退出。正常退出一般是由重由一个返回值为0 psented $ P $主。异常终止通常由非回零信号,但有非零codeS如何间preTED没有标准。另外,作为其他人所指出的,无效的主要()是明确的C ++标准禁止的,不得使用。有效的C ++ 签名是:

The return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signalled by a non-zero return but there is no standard for how non-zero codes are interpreted. Also as noted by others, void main() is explicitly prohibited by the C++ standard and shouldn't 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程序。无论是返回0应该被省略或未是公开辩论。有效的C程序主签名的范围要大得多。

It's also worth noting that in C++, int main() can be left without a return value 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.

此外,效率是不符合功能的问题。它只能进入和离开一次(标记节目开始和终止)如C ++标准。对于C的情况不同,重新进入的main()是允许的,但也许应该避免。

Also, efficiency is not an issue with the main function. It can only be entered and left once (marking program start and termination) according to the C++ standard. For C, the case is different and re-entering main() is allowed, but should probably be avoided.

这篇关于又该C和C ++的main()的回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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