Open In App

Different Ways To Terminate C++ Program

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A program must be terminated once its stated objectives have been met. Like any other language, C++ offers a variety of ways to terminate a program.

The following different methods will be discussed here to terminate a C++ program:

  1. abort() function
  2. terminate() function
  3. exit() function

Let’s start discussing each of these in detail.

abort()

abort function present inside a standard library header file. It terminates the program faster than other methods. The SIGABRT signal is raised by the abort function, which results in an abnormal termination of the current program. It is advised to utilize it only in the most extreme situations because it results in abnormal program termination.

The abort() function terminates the program without destroying the object and calling function passed to at_exit() or at_quick_exit() functions during the termination.

Syntax :

Void abort(void);

Parameter: This function doesn’t accept any parameter.

Return Value: This function doesn’t return any value.

Example 1: Below is the C++ program to abort the program after printing the welcome message.  

C++




// C++ program to abort
// program using abort()
#include<iostream>
using namespace std;
 
// Driver code
int main()
{
  cout << "Welcome To GeeksforGeeks !" <<
           endl;     
  abort(); 
   
  // This code will not execute
  cout << "A computer science portal "
}


Output:

Welcome To GeeksforGeeks !
Aborted

Explanation: In this program, the two print statements are used for two display messages and the abort function is called after one message. The abort () function is invoked when the first display message prints its output when the program gets executed. The program ends when the abort function is called. As a result, the sentence that follows the abort() method won’t be executed.

Example 2: Below is the C++ program for Abort() function in file handling.

C++




// C++ program for abort()
// function in file handling
#include <stdio.h>     
#include <stdlib.h>    
 
// Driver code
int main ()
{
  // Create a file pointer
  FILE * ptr;
  ptr = fopen ("sample.txt", "r");
   
  // Check file is NULL or not
  if (ptr == NULL)
  {
    fputs ("No such file found \n",
            stderr);
    abort();
  }
   
  // Close file
  fclose (ptr);
  return 0;
}


Output: 

No such file found 
Aborted

Explanation: This program checks if a given file is present or not. If the file is not present, then it will print the “not found” message and abort the program.

exit ()

The exit() function is declared in the <stdlib.h>standard library header file. The exit function is used to exit or terminate the program. The Exit function exits a program while ignoring the rest of the code.
In other words, statements or codes after the exit function declaration are ignored by the system or will not be executed.
EXIT_SUCCESS and EXIT_FAILURE are constants that are defined in header files to indicate exit success or exit failure of programmer code.

Syntax:

Exit (value of exit);

Exit(0);

Parameter: The exit function accepts a value as a Parameter. Every value except 0 indicates that the code was successfully terminated and every value except 0 indicates an error.

Return Value: The exit function doesn’t return a value.

Example 1: Below is the C++ program to implement exit():

C++




// C++ program to implement
// exit()
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
  cout << "Welcome To GeeksforGeek !" <<
           endl;
  exit(0);
   
  // This code will not execute
  cout << "A computer science portal ";
}


Output:

Welcome To GeeksforGeek !

Explanation: The exit () function is invoked when the first display message prints its output. The program ends when the exit function is called. When the exit function is called, it ignores the statement code after this function. hence the sentence that follows the exit() method won’t be executed.

Example 2: Below is the C++ program to implement exit() to check whether the number is 0 or not.

C++




// C++ program to implement
// exit()
#include<iostream>
using namespace std;
 
// Driver code
int main()
{
  // Input a number
  int no;
  cout << "Enter Number : ";     
  cin >> no;
   
  // Check number is 0 or not
  if(no != 0)
  {
    cout << "Valid input.\n";
  }
  else
  {
    cout << "Error Occurred!"
    exit(0);
  }
   
  // Print number
  cout << "The Number is : " << no;
}


Output: 

Enter Number : 7
Valid input.
The Number is : 7
Enter Number : 0
Error Occurred!

Explanation: When the user enters a positive value, it displays that value. However, if the user enters the value 0, this program will exit. and the statement following the exit() function is ignored by the system.

terminate() 

When the exception is thrown and not caught, When a constructor, destructor, static method, or thread throws an exception. Functions with the at_exit and at_quick_exit parameters raise an exception. An exception is thrown by the thread.
When the Terminate function is called in the program system, it calls the terminate_handler() function as well as the abort function. The terminate_handler() calls the abort function. The terminate function can be called directly by the program.

Syntax:

Void terminate();

Parameter: It doesn’t accept any parameters.

Return Value: It doesn’t have a return type.

Below is the C++ program to implement terminate():

C++




// C++ program to implement
// terminate()
#include<iostream>
using namespace std;
 
// Driver code
int main()
{
  cout << "Welcome To GeeksforGeeks !" <<
           endl;     
  terminate(); 
   
  // This code will not execute
  cout << "A computer science portal "
}


Output:

Abort signal from abort(3) (SIGABRT)

Output in Online Compiler:

Welcome To GeeksforGeeks !
terminate called without an active exception
Aborted


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads