开发者

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

开发者 https://www.devze.com 2023-02-07 13:27 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: main() functions return value?
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

main() functions return value?

What should main() return in C/C++?

What is the purpose of the returning of a value from main() in the languages C and C++?

How the return value hel开发者_JAVA百科ps us?

Can the return value be used to achieve anything important?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消