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.
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
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
printf ( "START" );
exit (0);
printf ( "End of program" );
}
|
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++
#include <bits/stdc++.h>
using namespace std;
void fun( void ) { cout << "Exiting" ; }
int main()
{
atexit (fun);
_Exit(10);
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(),
The code is immediately terminated after exit() is encountered. Now, if we replace exit with _Exit(),
C++
#include <bits/stdc++.h>
using namespace std;
void fun( void ) { cout << "Exiting" ; }
int main()
{
atexit (fun);
_Exit(10);
return 0;
}
|
No output
Last Updated :
27 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...