Node:The exit function, Next:, Previous:Function prototyping, Up:Functions

The exit function

GNU coding standards specify that you should always use exit (or return) within your main function. (See Style.)

You can use the exit function to terminate a program at any point, no matter how many function calls have been made. Before it terminates the program, it calls a number of other functions that perform tidy-up duties such as closing open files.

exit is called with a return code, like this:


In the example above, the return code is 0. Any program that calls your program can read the return code from your program. The return code is like a return value from another function that is not main; in fact, most of the time you can use the return command within your main, instead of exit.

Conventionally, a return code of 0 specifies that your program has ended normally and all is well. (You can remember this as "zero errors", although for technical reasons, you cannot use the number of errors your program found as the return code. See Style.) A return code other than 0 indicates that some sort of error has occurred. If your code terminates when it encounters an error, use exit, and specify a non-zero return code.