C programming abort stdlib.h

In C programming the <stdlib.h> abort function terminates the program abruptly.The declaration of the function is given below.

void abort(void);


Return type

Some points to note:

i) On executing the ‘abort’ function,for output stream buffer whether the data are flushed or the temporary files are removed is implementation-defined.The files are also not closed.

ii) The class destructor is not called if ‘abort’ is called untimely.

iii) In executing the ‘abort’ function the function pointed by the ‘atexit’ and ‘at_quick_exit’ parameter is not called.

Link: C atexit stdlib.h

iv) An implementation-defined status to signify an ‘unsuccessful termination’ is passed to the Operating System through ‘raise(SIGABRT)‘ call.

Code example

printf(“Testing ‘abort’ function”);


printf(“\nAfter calling abort”);

Output in VS,

Testing ‘abort’ function

A new window pop up with the message
“.. abort has been called”

As you can see the second line “printf(“\nAfter calling abort”);” is not executed because the program has been aborted in calling the ‘abort’ funtion.

A code example utilizing ‘abort’ function in a meaningful way is shown below.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> ///strlen function is include here

int main( )
char *c[]=”Sad string”;

char *cmem=(char*)malloc( strlen(c)+1 ); //allocated a memory of size same as the size of the ‘c’ string

memcpy( cmem , c , strlen(c)+1 );

//outputting the content of ‘cmem’
for(size_t i=0 ; i< strlen(c) ; i++)
printf(“%c” , *(mem+i) );

  abort(); //if allocation fail abort is called


return 0;


Sad string

The program is simple,if ‘malloc’ fails to allocate the storage abort() is called else you can carry out whatever instructions you want.In this case a string is copied to the memory and the content is printed out.

Related links

->C malloc stdlib.h

->C free stdlib.h