atexit() — プログラム終了の記録関数
フォーマット
#include <stdlib.h>
int atexit(void (*func)(void));
言語レベル
ANSI
スレッド・セーフ
はい
説明
atexit() 関数は、正常なプログラム終了時にシステムによって呼び出される func により示される関数を記録します。 atexit() 関数を使用して最大 32 の関数を登録し、移植性を改善できます。関数は後入れ先出し法で処理されます。 atexit() 関数は OPM デフォルトの活動化グループから呼び出すことができません。ほとんどの関数は atexit 関数で使用できますが、exit 関数が使用されると atexit 関数は失敗します。
戻り値
atexit() 関数は、正常に終了すると 0 を戻し、失敗するとゼロ以外の値を戻します。
例
次の例では atexit() 関数を使用してプログラム終了時に goodbye() を呼び出します。
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
void goodbye(void);
int rc;
rc = atexit(goodbye);
if (rc != 0)
perror("Error in atexit");
exit(0);
}
void goodbye(void)
/* This function is called at normal program end */
{
printf("The function goodbye was called at program end¥n");
}
/**************** Output should be similar to: ******************
The function goodbye was called at program end
*/