Exit Program

We can exit normal execution of a program by calling exit or abort. Normal execution ends with an exit code of zero. We call exit(0); and program exits with a success. However program can also return unexpectedly. These are the situations when program encounters erroneous situations like memory allocation failure or critical calculation errors like divide by zero etc. Programmers prefer calling exit with a proper error code. Windows/Linux has standard error numbers (errno) defined for every categories of errors. Programmers can even return the same error code received from the last system call. This error code will be saved in the process exit status code. Exit using abort is a different process. It raise a SIGABORT to the process and process exits as the default signal action taken by operating system. Process status will be updated accordingly. Parent process can obtain these status code and can decide if process completed successfully or aborted.


void abort(void);

Causes an abnormal program termination. Raises the SIGABRT signal and an unsuccessful termination status is returned to the environment. Whether or not open streams are closed is implementation-defined. No return is possible.


void exit(int status);

Causes the program to terminate normally. First the functions registered by atexit are called, then all open streams are flushed and closed, and all temporary files opened with tmpfile are removed. The value of status is returned to the environment. If status is EXIT_SUCCESS, then this signifies a successful termination. If status is EXIT_FAILURE, then this signifies an unsuccessful termination. All other values are implementation-defined. No return is possible.

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.