本文匯總 C 標準庫中以字母 **A** 開頭的常用函數,包含功能描述、原型聲明、頭文件、參數說明、返回值與最小可編譯示例,便於快速查閱。
| 函數 | 頭文件 | 功能 | 原型 | 返回值 |
|---|---|---|---|---|
abort | <stdlib.h> | 異常終止進程,不調用清理函數 | void abort(void); | 無返回 |
abs | <stdlib.h> | 整數絕對值 | int abs(int j); | 絕對值 |
absread/abswrite | <dos.h> | 絕對扇區讀/寫 (DOS 專用) | int absread(int drive, int nsects, int sectno, void *buf); | 0 成功,-1 失敗 |
access | <io.h>/<unistd.h> | 檢測文件訪問權限 | int access(const char *path, int mode); | 0 成功,-1 失敗 |
acos | <math.h> | 反余弦,定義域 [-1,1] | double acos(double x); | [0, π] 弧度 |
allocmem | <dos.h> | 分配 DOS 內存段 | int allocmem(unsigned size, unsigned *seg); | 段地址或 -1 |
arc | <graphics.h> | 繪制圓弧 (BGI) | void arc(int x, int y, int st, int end, int r); | 無 |
asctime | <time.h> | 將 struct tm 轉為字符串 | char *asctime(const struct tm *tm); | 靜態緩衝區指針 |
asin | <math.h> | 反正弦 | double asin(double x); | [-π/2, π/2] |
assert | <assert.h> | 斷言,條件為假則終止並輸出信息 | void assert(int expr); | 無 (調試版) |
atan | <math.h> | 反正切 | double atan(double x); | [-π/2, π/2] |
atan2 | <math.h> | 帶象限反正切 | double atan2(double y, double x); | [-π, π] |
atexit | <stdlib.h> | 註冊進程退出時調用的函數 (棧式) | int atexit(void (*func)(void)); | 0 成功 |
atof | <stdlib.h> | 字符串轉 double | double atof(const char *str); | 轉換結果 |
atoi | <stdlib.h> | 字符串轉 int | int atoi(const char *str); | 轉換結果 |
atol | <stdlib.h> | 字符串轉 long | long atol(const char *str); | 轉換結果 |
// access 檢測文件是否存在
#include
#include
int file_exists(const char *name) { return access(name, 0) == 0; }
// atexit 註冊多個清理函數 (後進先出)
#include
void cleanup1() { puts("cleanup 1"); }
void cleanup2() { puts("cleanup 2"); }
int main() { atexit(cleanup1); atexit(cleanup2); return 0; }
// 輸出: cleanup 2 → cleanup 1
// atof/atoi/atol 字符串轉數字
#include
double d = atof("3.14159");
int i = atoi("-42");
long l = atol("1234567890");
abort 不調用 atexit 註冊函數、不刷新流、不清理對象,僅用於嚴重錯誤立即停止。abs 僅處理 int,long 用 labs,long long 用 llabs,浮點用 fabs。access 存在 TOCTOU 競爭,安全敏感場景應直接嘗試打開並處理錯誤。asctime 返回靜態緩衝區,非線程安全;多線程用 asctime_r 或 strftime。assert 僅在 NDEBUG 未定義時生效,發布版自動消失。atof/atoi/atol 無錯誤報告機制 (非法輸入返回 0);健壯代碼請用 strtod/strtol/strtoll。absread/allocmem/arc) 僅適用於 16 位實模式環境,現代開發不建議使用。A 系列函數涵蓋進程控制、數學運算、時間轉換、斷言、字符串轉換、退出註冊等高頻領域。掌握原型、頭文件、返回值與避坑點,能顯著提升查閱效率與代碼健壯性。