3

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 helps us?

Can the return value be used to achieve anything important?

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452
  • There is a convention (especially in the Unix/Linux world) that an exit code of `0` is seem as program successful termination. It's useful for knowing how a program exited in cases where termination was not just `0`. http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c – wkl Jan 31 '11 at 16:35
  • You can read more about why returning a value in main is necessary here: http://users.aber.ac.uk/auj/voidmain.shtml – jluebbert Jan 31 '11 at 16:39

5 Answers5

11

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

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

fouronnes
  • 3,838
  • 23
  • 41
3

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
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
2

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.

Vlad
  • 35,022
  • 6
  • 77
  • 199
1

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

Community
  • 1
  • 1
mkaes
  • 13,781
  • 10
  • 52
  • 72