Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - atexit()

C library function - atexit()

C Standard Library - <stdlib.h> C Standard Library -

Description

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

Declaration

The following is the declaration of the atexit() function.



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

Parameters

Return Value

If the function is registered successfully, it returns zero; otherwise, it returns a nonzero value.

Example

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



#include 

#include 



void functionA ()

{

   printf("这是函数A\n");

}



int main ()

{

   /* Register termination function */

   atexit(functionA );

   

   printf("启动主程序...\n");



   printf("退出主程序...\n");



   return(0);

}

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



启动主程序...

退出主程序...

这是函数A

C Standard Library - <stdlib.h> C Standard Library -