Easy Tutorial
❮ C Function Memset C Unions ❯

C Library Function - atexit()

C Standard Library - <stdlib.h>

Description

The C library function int atexit(void (*func)(void)) invokes the specified function func when the program terminates normally. You can register your termination function anywhere, but it will be called upon program termination.

Declaration

Here is the declaration for the atexit() function.

int atexit(void (*func)(void))

Parameters

Return Value

The function returns zero if it is successfully registered, otherwise it returns a non-zero value.

Example

The following example demonstrates the use of the atexit() function.

#include <stdio.h>
#include <stdlib.h>

void functionA ()
{
   printf("This is functionA\n");
}

int main ()
{
   /* Register the termination function */
   atexit(functionA);

   printf("Starting main program...\n");

   printf("Exiting main program...\n");

   return(0);
}

Let's compile and run the above program, which will produce the following result:

Starting main program...
Exiting main program...
This is functionA

C Standard Library - <stdlib.h>

❮ C Function Memset C Unions ❯