What is the purpose of the returning of a value from main() in C/C++? [duplicate]

纵饮孤独 提交于 2019-11-29 15:15:37

It's the exit status. It's typically used to signal to the OS whether the process was successful or not in whatever it was doing.

It indicates the execution status of the program. Usually zero means it went fine.

On most operating systems, the return value becomes the value returned from the process when it exits. By convention, a return value of zero signifies a normal exit, while non-zero values signify different abnormal termination conditions.

This value can be tested by most command shells. For instance, in bash, the $? variable holds the return code of the last program. Also, its && and || operators determine whether to execute the right hand side, based on whether the left hand side signal a normal exit or not:

# Fetch the images, and only process them if the fetch succeeded (return code 0).
fetch-images && process-images

In command line environment, the return value signifies success or failure of the program run. This way you can share some status information with the caller. Zero is reserved to be success in Unix.

mkaes

A more detailed answer can also be found here. More Details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!