1

I've searched to find an answer to my specific question but could not find one that matched. I understand that return 0 and return EXIT_SUCCESS essentially do the same thing of saying that the program ended successfully, but what if you do return 1 or return 2 or any other int value for that matter? Do different int values mean different things to the compiler or is returning any int simply mean that the program terminated successfully, in which case it doesn't matter what int value you put in? To my (limited) knowledge, it seems that int main() just needs to return any int in order to terminate. Am I wrong?

Thanks and I apologize if this was asked before, I just wasn't able to find it.

floatfil
  • 357
  • 2
  • 7
  • 17
  • look at [this](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c), it explains it pretty well. The program will terminate regardless of whether or not you return `0`, the return value is just used to decipher how your program terminated (successful/unsuccessful and why, see [this](http://stackoverflow.com/questions/8626109/how-can-i-get-what-my-main-function-has-returned) as well) – wlyles Jul 10 '13 at 22:01
  • Hey here is a link that explains everything. hope that helps – Gabriel Jul 10 '13 at 22:15

2 Answers2

0

From: http://en.wikipedia.org/wiki/Exit_status

"The specific set of codes returned is unique to the program that sets it. Typically it indicates success or failure. The value of the code returned by the function or program may indicate a specific cause of failure. On many systems, the higher the value, the more severe the cause of the error.[1] Alternatively, each bit may indicate a different condition, which are then ored together to give the final value; for example, fsck does this."

In other words - 0 or EXIT_SUCCESS indicate that the program ran successfully. Other values (including EXIT_FAILURE) are used to indicate failure. Each individual program may optionally use different program defined codes to indicate different failure reasons.

Justin Meiners
  • 9,345
  • 6
  • 44
  • 90
0

It does matter. If it exited with a status of 0 or EXIT_SUCCESS, then you know your program reached the end of the main function correctly (or exit(0) was called). However, you can use exit(1) or return 1 at any point in the program to stop the program when it went somewhere it shouldn't have, meaning you had an error.

Different numbers can be used to identify different errors, so you may know exactly where the program exited, but in order to exit successfully, you should return 0 or EXIT_SUCCESS.