×

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions.

The exit() function in C/C++

The exit() method in the C language kills the calling process without running the remaining code.

The calling function is terminated instantly, and no more processes are run using the exit() function.

It terminates processes by calling the exit() method. It is defined in the C language's "stdlib.h" header file. It gives no results back.

The syntax for exit() is as follows: 

The value is returned to the parent process by the st_val.

void exit(int st_val) ;

Example

#include<bits/stdc++.h>
using namespace std ;
void my_function(void) {
   cout << "Exiting from program" ;
}
int main() {
   atexit(my_function) ;
   exit(10) ;
   cout<< "hello" ;
}

Output:

Difference between exit() and _Exit() in C++

The above program prints "Exiting from program." This is available in the my_function method.

Example 2

#include <stdio.h>
#include <stdlib.h>
int main() {
   int x = 10 ;
   printf("The value of x : %d\n", x) ;
   exit(0) ;
   printf("Calling of exit()") ;
   return 0 ;
}

Output:

Difference between exit() and _Exit() in C++

A variable 'x' is assigned a value in the above-displayed program. 

The printf() method prints the variable's value, and the exit() function is called. When exit() is called, the execution is instantly terminated, and the statement in the printf() is not printed.

The following commands are used to invoke exit():

 int x = 10 ;
 printf ("The value of x: %d\n", x) ;
 exit(0);

Example

// C/C++ example program for the exit() method.
#include <stdio.h>
#include <stdlib.h>
  
// the code logic
int main(void)
{
    printf("START") ;  //prints START
  
    exit(0);
    // 
When the exit(0) method is used, the program is ended.
  
    printf("End of program") ;  // the line mentioned will not be printed.
}
Difference between exit() and _Exit() in C++

Explanation: The printf function is called in the program above, and the value is printed there first.

The exit() method is then used, which immediately terminates the program and skips printing the statement in the printf().

The _Exit() method in C/C++

The C/C++ _Exit() method provides a common program shutdown without any cleanup activities. For instance, it doesn't run any atexit-registered functions.

When a process is terminated correctly, it returns control to the host environment using the function _Exit(). It doesn't carry out any cleanup operations.

The syntax for the _Exit() method is as follows :

void _Exit(int st_val) ;

The value returned to the parent process has the name st _val.

Here the exit_code represents the program's exit status, which can be 0 or non-zero.

Return Value: The _Exit() function returns nothing.

Example

#include <stdio.h>
#include <stdlib.h>
int main() {
   int x = 10 ;
   printf("The value of x : %d\n", x) ;
   _Exit(0) ;     //_Exit()
   printf("Calling of _Exit()") ;
   return 0 ;
}
Difference between exit() and _Exit() in C++

Example

// C++ program to demonstrate use of _Exit() method in C/C++
#include <stdio.h>
#include <stdlib.h>
  
// Driver Code
int main(void)
{
    int exit_code = 10 ; //assigning the value to exit_code
    printf("Termination using _Exit") ;
    _Exit(exit_code) ;
}
Difference between exit() and _Exit() in C++

Here, the result will be nothing.

The program doesn't produce any output, and this is because of the _Exit() method.

_Exit() vs exit()

The functionality of exit() and _Exit() in C/C++ is fairly similar.

Before ending the program, the exit() function performs cleanup tasks like connection closure and buffer flushes, and this is the sole distinction between exit() and _Exit().

Let's use an illustration to grasp the distinction further. We have used exit() in the following program.

Program: Using exit() method

// C++ program that displays differences
// between the exit() and the _Exit() methods
#include <bits/stdc++.h>
using namespace std ;
  
void funct(void) { cout << "Exiting" ; }
  
// main method code
int main()
{
    atexit(funct) ;
    exit(10) ; //exits the program
}

Output:

Difference between exit() and _Exit() in C++

When exit() is encountered, the program is instantly ended.

Now, we switch the exit() to the _Exit() method.

Program: Using _Exit() method

// C++ program that displays differences 
// between the exit() and the _Exit() method.
#include <bits/stdc++.h>
using namespace std ;
  
void funct(void) { cout << "Exiting" ; }
  
int main()
{
    atexit(funct) ;
    _Exit(10) ;
}
Difference between exit() and _Exit() in C++

Nothing is printed because there is no output.

The same program gives a different outcome for the exit() and _Exit() methods.


Related Topics

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.