C Programming/stdlib.h/abort

C programming standard library function .It is used in programming process or environment to stop the program or process abnormally.This function is required in the program when wrong condition gets encountered in program execution then to come out of process this function is used.

Introduction

edit

It deletes buffers and closes all open files before ending the program. The function abort() terminates the current program. Depending on the implementation, the return from the function can indicate a canceled (e.g. you used the signal() function to catch SIGABRT) or failed abort. SIGABRT is sent by the process to itself when it calls the abort libc function, defined in cstdlib. The SIGABRT signal can be caught, but it cannot be blocked; if the signal handler returns then all open streams are closed and flushed and the program terminates (dumping core if appropriate) , then it returns control to host environment.This means that the abort call never returns. Because of this characteristic, it is often used to signal fatal conditions in support libraries, situations where the current operation cannot be completed but the main program can perform cleanup before exiting. It is also used if an assertion fails.

Header files & Syntax

edit
 #include<stdlib.h>
 void abort( void );

Return Value

edit

This function does not return any value i.e. it is of void datatype.

Thread safety

edit

It is one of the thread safe functions from standard c library.i.e. function can be called by different threads without any problem.

Example

edit

This example tests for successful opening of the file myfile. If an error occurs, an error message is printed, and the program ends with a call to the abort() function.

#include <stdio.h>
#include <stdlib.h>
 int main(void)
{    FILE *stream;
     if ((stream = fopen("mylib/myfile", "r")) == NULL)
     {      perror("Could not open data file");
           abort();
     }
}