×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

C/C++ exit() vs _Exit(): Here, we are going to learn about the exit() and _Exit() functions with their usages, syntax, examples and differences between them.
Submitted by IncludeHelp, on June 18, 2020

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.

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.

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.

Reference: _Exit() function

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.