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

13 Jan, 2022
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 termination of the program like connection termination, buffer flushes etc.
In C, exit() terminates the calling process without executing the rest code which is after the exit() function. 
Syntax:
void exit(int exit_code); // The exit_code is the value which is returned to parent process
Example:
C
// C program to illustrate exit() function.#include <stdio.h>#include <stdlib.h>  // Driver Codeint main(void){    printf("START");      exit(0);    // The program is terminated here      // This line is not printed    printf("End of program");}
START
Explanation: In the above program, first the printf statement is called and the value is printed. After that, the exit() function is called and it exits the execution immediately and it does not print the statement in the printf().
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.
Syntax: 
void _Exit(int exit_code); // Here the exit_code represent the exit
                           // status of the program which can be 
                           // 0 or non-zero.
Return Value: The _Exit() function returns nothing.
CPP
// C++ program to demonstrate use of _Exit()#include <stdio.h>#include <stdlib.h>  // Driver Codeint main(void){    int exit_code = 10;    printf("Termination using _Exit");    _Exit(exit_code);}
Here, the output will be nothing.
Let’s understand the difference through an example. Here, in the following program, we have used exit(),
CPP
// A C++ program to show difference// between exit() and _Exit()#include <bits/stdc++.h>using namespace std;  void fun(void) { cout << "Exiting"; }  // Driver Codeint main(){    atexit(fun);    exit(10);}
Exiting
The code is immediately terminated after exit() is encountered. Now, if we replace exit with _Exit(), 
CPP
// A C++ program to show difference// between exit() and _Exit()#include <bits/stdc++.h>using namespace std;  void fun(void) { cout << "Exiting"; }  int main(){    atexit(fun);    _Exit(10);}
There is no output and nothing is printed.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Vishal Vishwakarma
anshikajain26
pratikcsc
CPP-Library
system-programming
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
fork() in C
Core Dump (Segmentation fault) in C/C++
Left Shift and Right Shift Operators in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects