main主函数执行完毕后,是否可能会再执行一段代码

3064阅读 1评论2011-05-02 dongfs_love
分类:C/C++

可以使用atexit()函数注册一个函数

  1. #include <stdlib.h>

  2. //功能:Processes the specified function at exit.
  3. //格式:int atexit( void ( __cdecl *func )( void ) );
  4. //描述:The atexit function is passed the address of a function (func) to be
  5. // called when the program terminates normally. Successive calls to atexit
  6. //create a register of functions that are executed in LIFO (last-in-first-out)
  7. //order. The functions passed to atexit cannot take parameters. atexit and
  8. //_onexit use the heap to hold the register of functions. Thus, the number of
  9. // functions that can be registered is limited only by heap memory.
  10. int atexit (void (*function)(void));


  11. #include <stdio.h>

  12. void fun1(void);

  13. void fun2(void);

  14. void fun3(void);

  15. void fun4(void);

  16. void main()

  17. {
  18.     
  19.     atexit(fun1);
  20.     
  21.     atexit(fun2);
  22.     
  23.     atexit(fun3);
  24.     
  25.     atexit(fun4);
  26.     
  27.     printf("this is executed first.\n");
  28.     
  29. }

  30. void fun1()
  31. {
  32.     printf("next.\n");
  33. }

  34. void fun2()
  35. {    
  36.     printf("executed ");    
  37. }

  38. void fun3()
  39. {    
  40.     printf("is ");    
  41. }

  42. void fun4()
  43. {    
  44.     printf("this ");    
  45. }
上一篇:a、b交换
下一篇:mxArray数据类型

文章评论