The Wayback Machine - https://web.archive.org/web/20231210205904/https://www.geeksforgeeks.org/exit-vs-_exit-c-cpp/
Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

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 <stdlib.h> header file.

Syntax of exit()

void exit(int exit_code);

Parameters

  • exit_code: It is the value that is returned to the parent process.

Return Value

  • The exit() function returns nothing.

Example of exit()

C




// C program to illustrate exit() function.
#include <stdio.h>
#include <stdlib.h>
 
// Driver Code
int main(void)
{
    printf("START");
 
    exit(0);
    // The program is terminated here
 
    // This line is not printed
    printf("End of program");
}


Output

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().

_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.

It is defined in the <stdlib.h> header file, which is part of the C Standard Library.

Syntax of _Exit()

void _Exit(int exit_code);

Parameters

  • exit_code: It represents the exit status of the program which can be 0 or non-zero.

Return Value

  • The _Exit() function returns nothing.

Example

C++




// C program to demonstrate the use of _Exit()
#include <stdio.h>
#include <stdlib.h>
 
// Driver Code
int main(void)
{
    // Define an exit code value
    int exit_code = 10;
 
    printf("Termination using _Exit");
 
    // Terminate the program using _Exit() function with the
    // specified exit code
    _Exit(exit_code);
 
    // The code after _Exit() will not be executed
 
    return 0;
}


No output

Difference between exit() and _Exit()

Let’s understand the difference through an example.

Here, in the following program, we have used 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"; }
 
// Driver Code
int main()
{
    // Register fun() to be called at program
    // termination
    atexit(fun);
 
    // Terminate the program with exit code 10
    exit(10);
 
    // The code after exit() will not be executed
 
    return 0;
}


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

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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 27 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads