Related Articles
























































Exit() Vs _Exit() In C++ With Examples

Prashant | Wed, 29 Jul, 2020 | 97

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

exit() function

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

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

ValueMacroDescription
0EXIT_SUCCESSIt defines that the termination is on the success of the program.
1EXIT_FAILUREIt 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.

Example of exit() function

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

Example of _Exit() function

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

Related Articles
























































0 comments
Leave a comment