Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

There are many ways how to exit program in C. Some of them are returning zero or error code, using exit function with EXIT_SUCCESS, EXIT_FAILURE, exit(0), exit(1) or some other error code, abort and probably few more I can't think of now.

My question is which option is the best and why. I'm asking here because every time I asked more experienced programmers this question, it ended up as a holy war.

share|improve this question
    
There is no such thing as "which is the best". You code according to your "exit" requirements. –  luis.espinal Apr 13 '11 at 13:24

2 Answers 2

I think the best thing is return EXIT_SUCCESS or return a non-zero number for which you have documented an error code, or EXIT_FAILURE if you can't define error codes for all the conditions that could cause you to fail. This would let a program using your program (in a pipe or batch function) actually use your error code to determine what to do next if applicable.

Personally I am not married to macros that always, on every single implementation on earth equal 0 but hey that sort of comment can get you downvoted.

share|improve this answer
    
Interesting. I heard people claim that in some implementations EXIT_SUCCESS and 0 may not be equal, but that both will signal normal program termination. On the other hand, I've heard lots of people say that always EXIT_SUCCESS==0 –  AndrejaKo Nov 3 '10 at 21:31
1  
Well its a puritanical thing. It is not specified to equal 0. But on every implementation it does equal 0 because historically it was always 0 and a lot of code out there still expects it to be 0. –  Jeremy Nov 3 '10 at 21:35
    
What about cleaning up after program and using return 0? –  AndrejaKo Nov 3 '10 at 22:57
    
every single implementation on earth equal 0 Counterexample: OpenVMS –  Thomas Eding Dec 12 '12 at 8:01

There is no one "best". If you want to maximize portability, you can only use three values: EXIT_SUCCESS, EXIT_FALURE, and 0 (and 0 and EXIT_SUCCESS mean the same thing). If you don't mind losing some portability, it's often useful to return various other values to indicate why something failed, or (in some cases) return a non-zero value even in case of success to indicate things like how many of X it found, or carry the result of a calculation, etc.

As such, you have a choice between portability (but mostly to systems you probably don't care about, chiefly VMS) and functionality on a slightly reduced set of systems. You have to decide which is more important to you.

Edit: No, EXIT_SUCCESS does not always equal zero -- but (at least as far as the standard cares) the two mean the same thing. There was at least one compiler on VMS, however, that defined EXIT_SUCCESS to a non-zero value (VMS normally interpreted even numbers as failure and odd numbers as success, so EXIT_SUCCESS was defined to the proper odd number, and 0 was treated specially, so the system got an odd number when/if you returned it).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.