abort() in C

The C abort() function is the standard library function that can be used to exit the C program. But unlike the exit() function, abort() may not close files that are open. It may also not delete temporary files and may not flush the stream buffer. Also, it does not call functions registered with atexit().

Syntax of abort() in C

void abort(void);

Parameters

  • The C abort() function does not take any parameter and does not have any return value. This function actually terminates the process by raising a SIGABRT signal, and your program can include a handler to intercept this signal.

Return Value

  • The abort() function in C does not return any value.

Example: C Program to Illustrate the abort() Function

C




// C program to demonstrate the syntax of abort function
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE* fp = fopen("C:\\myfile.txt", "w");
 
    if (fp == NULL) {
        printf("\n could not open file ");
        getchar();
        exit(1);
    }
 
    fprintf(fp, "%s", "Geeks for Geeks");
 
    /* ....... */
    /* ....... */
    /* Something went wrong so terminate here */
    abort();
 
    getchar();
    return 0;
}


Output

timeout: the monitored command dumped core
/bin/bash: line 1:    35 Aborted

Note: If we want to make sure that data is written to files and/or buffers are flushed then we should either use exit() or include a signal handler for SIGABRT.

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

The C exit(), abort(), and assert() functions are all used to terminate a C program but each of them works differently from the other. In this article, we will learn about exit, abort, and assert functions and their use in C programming language.

Similar Reads

1. exit() in C

The C exit() function is a standard library function used to terminate the calling process. When exit() is called, any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal....

2. abort() in C

...

3. assert() in C

...

Contact Us