歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Unix高級編程之-命令行參數(實踐一)

Unix高級編程之-命令行參數(實踐一)

日期:2017/3/1 10:21:46   编辑:Linux編程

1 atexit 函數

格式

#include <stdlib.h>

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

其中,atexit函數的參數是一個函數地址,當調用此函數時無需向他傳遞任何參數,也不期望他返回一個值。exit調用這些函數的順序與他們登記時候的順序相反。同一函數如若登記多次,則也會被調用多次。

2 實例

#include <stdio.h>


static void my_exit1(void);
static void my_exit2(void);
static void my_exit3(void);


int main(void)
{

if(atexit(my_exit2)!=0)
printf("can't register my_exit2");
if(atexit(my_exit1)!=0)
printf("can't register my_exit1");
if(atexit(my_exit3)!=0)
printf("can't register my_exit1");


printf("main is done\n");
return 0;
}


static void my_exit1(void)
{
printf("first exit handler\n");
}
static void my_exit2(void)
{
printf("second exit handler\n");
}
static void my_exit3(void)
{
printf("third exit handler\n");
}

3 結果

main is done
third exit handler
first exit handler
second exit handler

Copyright © Linux教程網 All Rights Reserved