Computer >> Computer tutorials >  >> Programming >> C programming

exit() vs _Exit() function in C and C++


In this section we will see what are the differences between exit() and _Exit() in C and C++. In C the exit() terminates the calling process without executing the remaining code which is present after exit() function.

In C++11, one new function is present called _Exit(). So what is the feature of this function? The exit() function performs some cleaning before terminating the program. It clears the connection termination, buffer flushes etc. This _Exit() function does not clean anything. If we test using atexit() method, it will not work.

Let us see two examples where at first we are using exit() function, then in the next

Example

#include<bits/stdc++.h>
using namespace std;
void my_function(void) {
   cout << "Exiting from program";
}
int main() {
   atexit(my_function);
   exit(10);
}

Output

Exiting from program

Example

#include<bits/stdc++.h>
using namespace std;
void my_function(void) {
   cout << "Exiting from program";
}
int main() {
   atexit(my_function);
   _Exit(10);
}

Output

In this case the output is blank. Nothing has come.