derek5.
2008-10-15 12:13
采纳率: 100%
浏览 373

在 c 和 c + + 中 main ()应该返回什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

16条回答 默认 最新

  • 旧行李 2008-10-15 12:16
    最佳回答

    The return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal exit 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()
    

    and

    int main(int argc, char* argv[])
    

    which is equivalent to

    int main(int argc, char** argv)
    

    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.

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

    评论
    解决 无用
    打赏 举报
查看更多回答(15条)

相关推荐 更多相似问题