exit(), abort() and assert() in C/C++



In C and C++ programming, the exit(), abort(), and assert() functions are used for program termination and debugging.

Each of these functions have different purpose and defined in different header files. In this article, we will learn these functions and their usage with the help of examples.

The exit() Function

In C/C++, exit() function is used to terminate the function call immediately without the executing further processes. This function is defined in <stdlib.h> (in C) and /<cstdio> (in C++) header file and does not return any value.

Syntax

Following is the basic syntax of exit() function:

void exit(int status_value);

Here,

  • status_value : This shows the termination status.

Example

In this example, a variable 'x' is initialized with a value. The value of variable is printed and exit() function is called. As exit() is called, it terminates the execution immediately and it does not print.

C C++
#include<stdio.h>
#include<stdlib.h>

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

   // terminate the program immediately
   exit(0);

   // this line will never run
   printf("Calling of exit()");
   return 0;
}

Output

The above program produces the following result:

The value of x : 10
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

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

   // terminate the program immediately
   exit(0);  

   // This line will never run
   printf("Calling of exit()");
    
   return 0;
}

Output

The above program produces the following result:

The value of x : 10

The abort() Function

The abort() function terminates the execution abnormally. It belongs to "<stdlib.h>/<cstdlib>" header file. For programmer, it is suggested that not to use this function for termination.

Syntax

Here is the basic syntax of abort() function:

void abort(void);    

Example

In the below program, a variable 'a' is initialized with the value and printed. As the abort() is called, it terminates the execution immediately but abnormally.

C C++
#include<stdio.h>
#include<stdlib.h>
int main() {
   int a = 15;
   printf("The value of a : %d\n", a);

   // this function forcefully terminate the program
   abort();

   // This line will never be executed
   printf("Calling of abort()");
   return 0;
}

Output

The above program produces the following result:

The value of a : 15
Aborted (core dumped)
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main() {
   int a = 15;
   printf("The value of a : %d\n", a);

   // this function forcefully terminate the program
   abort();  

   // This line will never be executed
   printf("Calling of abort()");

   return 0;
}

Output

The above program produces the following result:

The value of a : 15
Aborted (core dumped)

The assert() Function

The assert() function evaluates the expressions given as argument. If expression is true, it doesn't return anything. If expression is false, it abort the execution. This function is defined in "assert.h" header file and you need to include it to use this function.

Syntax

Following is the basic syntax of assert() function:

void assert(int exp);

Here,

  • exp : The expression you want to evaluate.

Example

In this example, a variable 'a' is initialized with a value. The value of variable is printed and assert() function is called. As assert() is called, it evaluates the expression that 'a' is not equal to 15 which is false that is why it aborts the execution and shows an error.

C C++
#include<stdio.h>
#include<assert.h>

int main() {
   int a = 15;
   printf("The value of a : %d\n", a);

   // This will fail because a is 15
   assert(a!=15);

   // This line won't run
   printf("Calling of assert()");
   return 0;
}

Output

The above program produces the following result:

The value of a : 15
main: main.c:6: main: Assertion `a!=15' failed.
Aborted (core dumped)
#include<iostream>
#include<cstdio>
#include<cassert>
using namespace std;

int main() {
   int a = 15;
   printf("The value of a : %d\n", a);

   // This will fail because a is 15
   assert(a != 15);  
   
   // This line won't run
   printf("Calling of assert()\n");  

   return 0;
}

Output

The above program produces the following result:

The value of a : 15
main: main.c:6: main: Assertion `a!=15' failed.
Aborted (core dumped)

Differences exit(), abort() and assert() Functions

Here, we listed the feature of all the above function in a single table:

exit() abort() assert()
The exit() terminates the program normally. The abort() terminates the program abnormally. The assert() is applied for condition; if it is false then terminate the program.
It belongs to <stdlib.h> / <cstdlib> header file. It belongs to <stdlib.h> / <cstdlib> header file. It belongs to <assert.h> / <cassert> header file.
The status code returned to operating system. It does not return any status code. It does not return any status code.
Updated on: 2025-06-04T17:30:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements