1、abort
定義:
#include
void abort(void);
作用:強(qiáng)行終止程序 (異常終止);如果當(dāng)前shell不限制ulimit,將會(huì)core dump。
2、assert宏
原型:
#include
void assert(int expression);
作用:計(jì)算 expression的值,若其返回0(假),則向 stderr打印出錯(cuò)信息,并調(diào)用 abort終止程序。
使用注意事項(xiàng):assert一般在開(kāi)發(fā)階段用于調(diào)試。為防止定義NEBUG后assert被禁用,最好不要直接向assert進(jìn)行輸入,而是如下所示:
p = malloc (sizeof (char) *100);
assert(p);
3、exit
原型:
#include
void exit(int status);
作用:返回一個(gè)狀態(tài)值給操作系統(tǒng),status在stdlib.h中定義了EXIT_SUCCESS和EXIT_FAILURE。
4、atexit
原型:
#include
int atexit(void (*function)(void));
作用:注冊(cè)一個(gè)函數(shù),這個(gè)函數(shù)可以定義一些操作,用來(lái)在程序正常退出時(shí)執(zhí)行之。
atexit注冊(cè)成功則返回0,否則返回1;可以用“,”隔開(kāi)注冊(cè)多個(gè)函數(shù),執(zhí)行順序?yàn)樽钭筮叺淖詈髨?zhí)行。
5、errno變量:
定義: #include ;
int errno;
作用:全局變量,Linux系統(tǒng)調(diào)用與大部分庫(kù)函數(shù)設(shè)置該值,errno.h定義了其值對(duì)應(yīng)的錯(cuò)誤。例如ENOENT代表No such file or directory等。
函數(shù)perror可以打印相應(yīng)的出錯(cuò)信息。
注意事項(xiàng):很多函數(shù)返回并設(shè)置errno后并不會(huì)把之清0,如果調(diào)用一個(gè)可能在出錯(cuò)時(shí)設(shè)置errno的庫(kù)函數(shù)的時(shí)候,最好先手動(dòng)把errno清零。
6、strerror
原型:
#include
char *strerror(int errnum);
作用:把errno轉(zhuǎn)換成標(biāo)準(zhǔn)的出錯(cuò)信息。例如: ps = strerror(ENOENT);
則指針ps指向的字符串為"No such file or directory "等。
7、perror
原型:
#include
#include
void perror(const char *s);
作用:打印s所指的字符串和標(biāo)準(zhǔn)出錯(cuò)信息,相當(dāng)于
printf("%s: %s\n", *s, strerror(errno));