What the reasons for/against returning 0 from main in ISO C++?
I know that the C++ standard says that return 0
is inserted at the end of main()
if no return statement is given; however, I often see recently-written, standard-conforming C++ code that explicitly returns 0 at the end of main()
. For what reasons would somebody want to explicitly return 0 if it's automatically done by the compiler?
因为从具有非无效返回类型的函数中“返回”某些东西(即使标准说它不是绝对必要的),它看起来很奇怪。
By being explicit you are explicitly showing your intent.
By relying on something implicit you could have 2 cases: 1) You intended it, 2) You forgot it.
Makes it clear to other programmers that you didn't just forget to put the return statement there.
Some compilers may issue a warning if you don't return anything.
Shows explicitly what the function returns.
Many programmers don't know about this rule.