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

后端 未结 9 1555
佛祖请我去吃肉
佛祖请我去吃肉 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:46

    Just because your code complies with the standard, who says your code is going to be run through a compliant compiler? Believe it or not, people do use compilers besides just recent versions of GCC and Visual C++.

    And of course there's the explicit intent thing that everyone else has mentioned.

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

    I often do it because I often compile code for straight C, so I either type it in out of habit or because the snippet I created main() from has the explicit return. There's no reason to remove it, so it usually stays.

    Then again, there are times when I won't bother typing it in (maybe I realized I didn't need it) or I may have used a different snippet.

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

    Because some people don't know. Not necessarily the people who wrote that code (although that's also possible), but some people out there. Explicitly writing return 0; is being nice to them.

    Also, as a convention it makes the language more uniform, which aesthetically is important to at least me.

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