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.

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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.