Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - The exit function

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:

exit(0);

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.

 
 
  Published under free license. Design by Interspire