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

In C programming, the exit(), abort(), and assert() functions are used for program termination and debugging. Each function serves a different purpose and is defined in different header files.

The exit() Function

The exit() function is used to terminate a program immediately in a normal way. It is defined in the <stdlib.h> header file and allows the program to return a status code to the operating system.

Syntax

void exit(int status_value);

Parameters:

  • status_value − The termination status code (0 indicates success, non-zero indicates error)

Example

In this example, the program prints a value and then terminates using exit(0), preventing any code after it from executing −

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

int main() {
    int x = 10;
    printf("The value of x: %d\n", x);
    
    // Terminate the program normally
    exit(0);
    
    // This line will never execute
    printf("This won't print");
    return 0;
}
The value of x: 10

The abort() Function

The abort() function terminates the program abnormally without performing cleanup operations. It is also defined in <stdlib.h> and is typically used when an unrecoverable error occurs.

Syntax

void abort(void);

Example

The following program demonstrates abnormal termination using abort()

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

int main() {
    int a = 15;
    printf("The value of a: %d\n", a);
    
    // Forcefully terminate the program
    abort();
    
    // This line will never be executed
    printf("This won't print");
    return 0;
}
The value of a: 15
Aborted (core dumped)

The assert() Function

The assert() function is a debugging macro that evaluates an expression. If the expression is false, it prints an error message and terminates the program. It is defined in <assert.h>.

Syntax

void assert(int expression);

Parameters:

  • expression − The condition to evaluate

Example

This example shows how assert() terminates the program when a condition fails −

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

int main() {
    int a = 15;
    printf("The value of a: %d\n", a);
    
    // This assertion will fail because a equals 15
    assert(a != 15);
    
    // This line won't execute
    printf("This won't print");
    return 0;
}
The value of a: 15
Assertion failed: a != 15, file main.c, line 8
Aborted (core dumped)

Comparison of Functions

Function Termination Type Header File Status Code Use Case
exit() Normal <stdlib.h> Returns status code Controlled termination
abort() Abnormal <stdlib.h> No status code Emergency termination
assert() Conditional abnormal <assert.h> No status code Debugging and testing

Conclusion

Use exit() for normal program termination, abort() for emergency situations, and assert() for debugging and validating assumptions during development. Each serves a specific purpose in program control flow.

Updated on: 2026-03-15T09:59:07+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements