C++

How to Use the abort() Function in C++ with Examples

Every programming language establishes some fundamental commands to initiate, process and terminate its programs. The abort function is required to end any program. This article describes abort() function, its syntax, and examples.

What is an abort() function?

The abort() function is used to end any program abruptly. It raises the SIGABRT signal to initiate termination of any current program. The abort function is in the stdlib.h header file and therefore the program needs to include this header file before executing this function.

Syntax

void abort(void)

Parameters
The abort() function is used for termination of the program and therefore no input of parameters is involved.

Return
The abort() function has no return value.

Example1: C++ Program for abort() after Welcome Message

This program shall display a welcome message and then will abort as soon the abort() is called. The rest of the code mentioned after the abort() shall not be executed:

#include<iostream>
using namespace std;
 
int main()
{
  cout << "Welcome To Linux Hint !" <<
           endl;    
  abort();
   
  // After calling abort () function, The below included code shall not execute
  cout << "A Learning portal ";
}

The above code starts with namespace std and then defines the main function of displaying the welcome message. The abort() is called right after the welcome message which terminates the program at this point. The remaining code is not executed since the abort() has been already called.

Example2: C++ Program for abort() while Displaying Invalid Number

This program checks whether the entered number is a non-zero integer or not. The abort() function has been called before the final display message in case the number entered is zero:

#include<iostream>
using namespace std;
 
int main()
{
  // Enter a valid number
  int no;
  cout <> no;
   
  // Check number is 0 or not
  if(no != 0)
  {
    cout << "The number is non-zero.\n";
  }
  else
  {
      cout << "The number is zero!\n";
      abort();  
   
   
  }
   
  // Print number
  cout << "The Number is : " << no;
}

If a non-zero integer is entered, the program will display first that the number is non-zero. Finally, the number will be displayed on the screen.

If zero is entered, the program shall display ‘The number is zero!’. However, use of abort() abruptly terminates the program before the last thread of program is executed for displaying the entered zero number as in the non-zero case.

Example3: C++ Program for abort() in Comparison of Two Integers for Multiplications

The below program compares A & B before the multiplication syntax. If A>B, the program shall run and display the final number. If A<B, the program will abort before the display of the final message.

#include<iostream>
using namespace std;
 
int main()
{
  // Enter a valid number
  int A;
  int B;
  int C;
 
  cout <> A;
 
  cout <> B;
   
  // Check number is 0 or not
  if(A>B)
  {
    cout <B \n";
    C=A*B;
  }
  else
  {
      cout << "
Condition is false: A<B \n";
      abort();  
  }
   
  // Print number
  cout << "
The Number is : " << C;
}

The above code starts with stdlib.h in the header, and it defines three variables; A, B & C. If A is greater than B, the program will display valid input and the final number.

If A is less than B, the program will display invalid input and abort without displaying the final number in output:

Conclusion

The abort() function is used to abort the program at any point. It can be called in a variety of programs which may include displaying messages, if-else conditions, validating the numbers or more.

About the author

Anwar Mustafa

I am a technical content writer with a master's degree in electrical engineering. With a unique blend of engineering expertise and exceptional writing skills, I craft engaging and informative content.