Difference between exit() and _Exit()

Let’s understand the difference through an example.

Here, in the following program, we have used exit(),

C++





Output

Exiting

The code is immediately terminated after exit() is encountered. Now, if we replace exit with _Exit()

C++




// A C++ program to demonstrate the difference between
// exit() and _Exit()
#include <bits/stdc++.h>
using namespace std;
 
// Function registered with atexit()
void fun(void) { cout << "Exiting"; }
 
int main()
{
    // Register fun() to be called at program termination
    atexit(fun);
 
    // Terminate the program immediately using _Exit()
    // function with exit code 10
    _Exit(10);
 
    // The code after _Exit() will not be executed
 
    return 0;
}


No output



exit() vs _Exit() in C/C++

exit() and _Exit() in C/C++ are very similar in functionality. However, there is one difference between exit() and _Exit() and it is that exit() function performs some cleaning before the termination of the program like connection termination, buffer flushes, etc.

Similar Reads

exit()

In C, exit() terminates the calling process without executing the rest code which is after the exit() function call. It is defined in the header file....

_Exit()

...

Difference between exit() and _Exit()

The _Exit() function in C/C++ gives normal termination of a program without performing any cleanup tasks. For example, it does not execute functions registered with atexit() function....