可以使用atexit()函数注册一个函数
- #include <stdlib.h>
- //功能:Processes the specified function at exit.
- //格式:int atexit( void ( __cdecl *func )( void ) );
- //描述:The atexit function is passed the address of a function (func) to be
- // called when the program terminates normally. Successive calls to atexit
- //create a register of functions that are executed in LIFO (last-in-first-out)
- //order. The functions passed to atexit cannot take parameters. atexit and
- //_onexit use the heap to hold the register of functions. Thus, the number of
- // functions that can be registered is limited only by heap memory.
- int atexit (void (*function)(void));
- #include <stdio.h>
- void fun1(void);
- void fun2(void);
- void fun3(void);
- void fun4(void);
- void main()
- {
-
- atexit(fun1);
-
- atexit(fun2);
-
- atexit(fun3);
-
- atexit(fun4);
-
- printf("this is executed first.\n");
-
- }
- void fun1()
- {
- printf("next.\n");
- }
- void fun2()
- {
- printf("executed ");
- }
- void fun3()
- {
- printf("is ");
- }
- void fun4()
- {
- printf("this ");
- }