×

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

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes 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.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.