Prashant | Wed, 29 Jul, 2020 | 96
exit() function is used to terminate the program normally with several cleanups like buffers flushing, terminating the connections, etc before exiting from the program.
Syntax:
void exit(int status);
_Exit() function is also used to terminate the program normally without cleanups like buffers flushing, terminating the connections, etc before exiting from the program.
It was introduced in C11.
Syntax:
void _Exit(int status);
Parameter(s): status – defines the exit status.
There are two status that we can define,
Value | Macro | Description |
---|---|---|
0 | EXIT_SUCCESS | It defines that the termination is on the success of the program. |
1 | EXIT_FAILURE | It defines that the termination is on the failure of the program. |
Return value: The return type of the function is void, it returns nothing.
Note: In both functions, if the value of status is 0 or EXIT_SUCCESS, an implementation-defined status indicating successful termination is returned to the host environment. If the value of status is EXIT_FAILURE, an implementation-defined status, indicating unsuccessful termination, is returned. In other cases, an implementation-defined status value is returned.
// C++ program to demonstrate the
// example of exit()
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello, world...");
exit(0);
printf("Bye, bye!!!...");
return 0;
}
Output:
Hello, world...
// C++ program to demonstrate the
// example of _Exit()
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello, world...");
_Exit(0);
printf("Bye, bye!!!...");
return 0;
}
Output:
Explanation:
See both of the examples, in Example 1, there is a printf() function before the exit(0) which will terminate the program. As I told, exit() function cleanups the several things thus it will also flush the output stream and "Hello, world..." will be printed. In Example 2, _Exit(0) will terminate the program and will not clean up the things thus it will also not flush the output stream and nothing will be printed.