exit() and _Exit() in C and C++
Introduction:
In computer programming, it is essential to terminate a program or process under certain circumstances. The exit function is used to terminate the execution of a program. In C and C++, there are two exit functions available: exit() and _Exit(). These functions allow a program to exit the execution, with or without cleaning up resources allocated by the program. This article will provide an overview of the exit() and _Exit() functions, their differences, and when to use them.
What is exit() function?
The exit() function is a library function in C and C++ that is used to terminate the execution of a program. This function is defined in the stdlib.h header file in C and cstdlib header file in C++. The exit() function takes an integer value as its argument, which is returned to the operating system upon the termination of the program. The exit() function performs essential tasks before terminating the program, such as closing open files, flushing output buffers, and deallocating memory.
Syntax of exit() function:
void exit(int status);
The integer value passed to the exit() function is called an exit status. The value of the exit status is usually 0 if the program terminates successfully and a non-zero value if the program terminates abnormally. The exit status can be retrieved by the operating system to determine the reason for termination.
Example program using exit() function:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Before exit() function\n");
exit(0);
printf("After exit() function\n");
return 0;
}
Explanation:
- This program demonstrates the use of the exit() function in C.
- The stdio.h and stdlib.h headers are included to provide access to the standard input/output functions and the exit() function, respectively.
- In the main() function, the string "Before exit() function" is printed to the console using the printf() function.
- The exit() function is then called with an argument of 0, indicating successful termination.
- The exit() function terminates the program immediately and passes the specified status code (in this case, 0) back to the operating system.
- Since the exit() function terminates the program, any code that comes after it will not be executed. Therefore, the next line, which contains the string "After exit() function", will not be printed to the console.
Program Output:

What is _Exit() function?
The _Exit() function is similar to the exit() function in C and C++. However, the _Exit() function does not perform any cleanup tasks before terminating the program. This function is defined in the stdlib.h header file in C and cstdlib header file in C++.
Syntax of _Exit() function:
void _Exit(int status);
The integer value passed to the _Exit() function is called an exit status, which is returned to the operating system upon the termination of the program. The value of the exit status is usually 0 if the program terminates successfully and a non-zero value if the program terminates abnormally. The _Exit() function is used when there is no need to perform any cleanup tasks before terminating the program.
Example program using _Exit() function:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Before _Exit() function\n");
_Exit(0);
printf("After _Exit() function\n");
return 0;
}
Explanation:
- This program demonstrates the use of the _Exit() function to terminate a program immediately.
- The program starts by printing "Before _Exit() function" to the console.
- Next, the _Exit() function is called with an exit status of 0. This causes the program to terminate immediately without executing any further code.
- Therefore, the second printf statement ("After _Exit() function") is never executed.
- Finally, the program returns 0 to the operating system to indicate successful termination.
Program Output:

Differences between exit() and _Exit() functions
The exit() and _Exit() functions have some key differences:
- The exit() function performs some essential tasks, such as closing open files, flushing output buffers, and deallocating memory, before terminating the program. The _Exit() function does not perform any of these tasks.
- The exit() function allows a program to terminate with cleanup tasks, whereas the _Exit() function terminates the program immediately without any cleanup tasks.
- The exit() function can be overridden by an atexit() function, which is executed before the program exits. The _Exit() function cannot be overridden by an atexit() function.
When to use exit() and _Exit() functions
The choice between exit() and _Exit() functions depends on the situation and the requirements of the program.
In general, use the exit() function when you want to terminate the program after performing cleanup tasks. For example, if your program has opened files, you should close them before terminating the program using the exit() function. This will ensure that the data is written to the files before the program terminates.
On the other hand, use the _Exit() function when you want to terminate the program immediately without performing any cleanup tasks. For example, if your program has encountered a critical error, you should terminate it immediately using the _Exit() function.
Common Use Cases for exit() and _Exit()
Here are some common situations where you might use the exit() or _Exit() functions:
- To terminate the program when an unrecoverable error occurs.
- To return an exit status to the operating system indicating the success or failure of the program execution.
- To terminate the program when a specific condition is met, or a specific command is executed, such as when a user types "exit" in a command-line interface.
- To terminate a program from a function that is called from multiple places in the program.
- To terminate a program when a specific signal is received by the program.
- To terminate a program in a multithreaded application, where different threads need to terminate independently.
Conclusion
In summary, the exit() and _Exit() functions in C and C++ are used to terminate a program's execution. The exit() function performs some essential tasks, such as closing open files, flushing output buffers, and deallocating memory, before terminating the program. On the other hand, the _Exit() function terminates the program immediately without performing any cleanup tasks.
In general, you should use the exit() function when you need to perform some cleanup tasks before terminating the program, such as closing open files or deallocating memory. On the other hand, you should use the _Exit() function when you need to terminate the program immediately without performing any cleanup tasks.
Both exit() and _Exit() functions allow a program to return an exit status to the operating system, indicating the success or failure of the program execution. It is important to choose the appropriate function depending on the situation and the desired behaviour of the program.