We often write large and complex programs. We allocate memory, open files/ resources and lots of other things. Most of the time our program executes in proper environment and encounters less/no error. We should handle this errors and exit properly.

Exiting from a logic or program involves freeing up memories and closing handles and files. We often write a cleanup handler which does this work. once cleanup is done we can exit gracefully. This cleanup ensures no memory and resource leak.

C runtime provides a atexit() API to register for cleanup handlers.

int atexit(void (*function)(void));

User need to implement these callbacks and C runtime will call all registered callbacks in reversed order. Maximum 32 routines can be registered for one application.

#include <stdlib.h>

FILE* fp = NULL;
char *hostname = NULL;

void free_memory(void)
  printf ("Cleaning memory\n");
  if (hostname) {
    free (hostname);

void close_files(void)
  printf ("Closing files\n");
  if (fp) {
    fclose (fp);

int main(int argc, char* argv[])
  int err;
  err = atexit (free_memory);
  if (err != 0) {
    fprintf (stderr, "cannot set exit function\n");
  err = atexit (close_files);
  if (err != 0) {
    fprintf(stderr, "cannot set exit function\n");
  hostname = (char *) malloc(HOST_NAME_MAX);
  if (hostname == NULL) {
    fprintf(stderr, "Error in memory allocation, exiting.\n");
  err = gethostname (hostname, HOST_NAME_MAX);
  if (err != 0) {
    fprintf(stderr, "Error %d executing gethostname(), exiting.\n", err);
  fp = fopen ("hostinfo.txt", "w");
  if (fp == NULL) {
    fprintf (stderr, "Error writing to file, exiting.\n");
    exit (-1);
  fprintf (fp, "Hostname : %s\r\n", hostname);
  /* try to open a non-existent file */ 
  if (fopen ("foo", "r") == NULL) {
    fprintf (stderr, "Oops some error happened, exiting!\n");
    /* additional lines here to write more info */
  /* ........................................ */
  /* Finally close the file */
  free (hostname);


Oops some error happened, exiting!
Closing files
Cleaning memory

About our authors: Team EQA

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

Further readings

What is argc, argv in main function in c?[Explain]
Argc and argv are argument count and argument vector. Argc=number of arguments passed. Argv=Aarray of pointers, holds individual arguments. More..

Who calls main() function? What is C startup routine and assembly, explain?
Explains how control comes from C startup routine and assembly code to main routine. Setting up argc argv, environment variables before main

How does shell pass paramaters to my C program? [details of argv and environ]
How shell passes arguments and environment? Argc=number of arguments. Argv=Aarray of pointers, environ=Array of environment srtings. More..

How to create a child process with C library call system()?
How to create a child process in C? Talks about system() library function. Execute a shell command from your app. Check the status of child

Learn on Youtube