abort() Function in C with Example

This Tutorial explains the abort() function in C with examples.

What is abort() Function?
abort() Function in C terminates execution of a program abnormally. It’s defined in ‘stdlib.h’ header and is prototyped below

Prototype of abort() Function in C

    void abort(void);

Example of abort() Function in C

/* abort.c -- terminates execution abnormally */
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    abort();
    printf("\"abort() called prior to printf()\"\n");
 
    return 0;
}

When “abort()” is called, it raises the “SIGABRT” signal, which results in an abnormal program termination. A handler can be installed by a program to carry out required actions either before or in place of the program’s termination.

advertisement
advertisement

The following two functions relate with normal termination of a program.

    int atexit(void (*function)(void));
    int exit(int status);
  • These are defined in ‘stdlib.h’ header.
  • ‘atexit()’ registers ‘functions as exit functions’. ‘atexit()’ returns 0 for successfully registring otherwise retuns non-zero.
  • When ‘exit()’ function is called, program is about to terminate normally.
  • All the functions registered with ‘atexit()’ are called in the reverse order that they were registered.
  • Then all buffers are flushed for streams that need it, all open files are closed.
  • Any file created with ‘tmpfile()’ is deleted. Recall that ‘tmpfile()’ creates a file for temporary holding the data in a program.
  • File is deleted if you explicitly close it or when program is done.

Let’s take a simple C program using ‘atexit()‘ and ‘exit()‘ functions,

Note: Join free Sanfoundry classes at Telegram or Youtube
/* atexit_exit.c -- program uses 'atexit()' to register functions */
/* as exit() functions */
#include <stdio.h>
#include <stdlib.h>
 
void goodbye(void);
void okey(void);
 
int main(void)
{
    int ret;
 
    /* atexit() registers goodbye() as exit function */
    ret = atexit(goodbye);
    if (ret == 0)
    puts("atexit() succeeds!");
 
    /* atexit() registers okey() as exit function */
    ret = atexit(okey);
    if (ret == 0)
    puts("atexit() succeeds!");
 
    exit(0);
}
 
void goodbye(void)
{
    puts("goodbye!");
}
 
void okey(void)
{
    puts("okey, bye!");
}

Output follows

advertisement
atexit() succeeds!
atexit() succeeds!
okey, bye!
goodbye!

Notice that firstly, ‘atexit()’ registered ‘goodbye()’ and ‘okey()’ functions and when ‘exit()’ was called functions registered with ‘atexit()’ called in reverse order of their prior registration and printed massages on stdout. Also, notice that effect of calling ‘exit()’ is same as ‘main()’ returns to host environment. Further, when ‘exit()’ is called, program terminates normally and therefore ‘exit()’ never returns to calling program.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.