What the reasons for/against returning 0 from main in ISO C++?

后端 未结 9 1554
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 19:12

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-wri

相关标签:
9条回答
  • 2021-01-20 19:28

    It makes behavior of the code explicit.

    0 讨论(0)
  • 2021-01-20 19:34

    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.

    0 讨论(0)
  • 2021-01-20 19:38

    Because this is how they did it 30 years ago. It is more of a convention IMO.

    0 讨论(0)
  • 2021-01-20 19:40

    Because it just looks weird to not "return" something from a function having a non-void return type (even if the standard says it's not strictly necessary).

    0 讨论(0)
  • 2021-01-20 19:40
    • 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.

    0 讨论(0)
  • 2021-01-20 19:44

    Misunderstanding. There's simply no reason to, and if someone doesn't know that they'll add return 0;.

    0 讨论(0)
提交回复
热议问题