C++ void return type of main()

后端 未结 8 2400
灰色年华
灰色年华 2020-12-11 01:58

Some C++ compilers allow the main function to have return type void. But doesn\'t the Operating System require int type value returned to specify

相关标签:
8条回答
  • 2020-12-11 02:22
    But doesn't OS require int type value returned to specify whether program 
    ended well or not?
    

    Why would it always? On windows when you double click on the icon, the process dies after it ends. OS do not check for the return type there. Even on linux if you just run the binary as ./runBinary, it simply runs and exits. The OS do not show message by itself that it fails or succeeds.

    All the above answers are right that the standard says it is int, but some compilers allow void too.

    0 讨论(0)
  • 2020-12-11 02:23

    Depending on the compiler, you may be able to use a void main function, however the proper way (that a truly standard compliant compiler should follow) is to return int with 0 being a nice & clean exit and anything else indicating that your program has done something wrong.

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