C 庫函數 - atexit()

C 標準庫 - <stdlib.h> C 標準庫 - <stdlib.h>

描述

C 庫函數 int atexit(void (*func)(void)) 當程式正常終止時,調用指定的函數 func。您可以在任何地方註冊你的終止函數,但它會在程式終止的時候被調用。

聲明

下麵是 atexit() 函數的聲明。

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

參數

  • func -- 在程式終止時被調用的函數。

返回值

如果函數成功註冊,則該函數返回零,否則返回一個非零值。

實例

下麵的實例演示了 atexit() 函數的用法。

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

void functionA ()
{
   printf("這是函數A\n");
}

int main ()
{
   /* 註冊終止函數 */
   atexit(functionA );

   printf("啟動主程序...\n");

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

   return(0);
}

讓我們編譯並運行上面的程式,這將產生以下結果:

啟動主程序...
退出主程序...
這是函數A

C 標準庫 - <stdlib.h> C 標準庫 - <stdlib.h>