本文匯總 C 標準庫及常見擴展庫中以 字母 A 開頭 的函數,包含功能說明、函數原型、頭文件要求與最小可運行示例。適用於快速查閱與參考。
#include
void abort(void);
// 不執行清理(atexit、緩衝區刷新),直接終止,返回非零碼給宿主環境。
int main() {
printf("Calling abort()\n");
abort();
return 0; // 永不執行
}
#include
int abs(int i);
// 返回 |i|,注意 INT_MIN 絕對值溢出為未定義行為。
int main() {
int n = -1234;
printf("abs(%d) = %d\n", n, abs(n));
}
#include
int absread(int drive, int nsects, int sectno, void *buffer);
int abswrite(int drive, int nsects, int sectno, void *buffer);
// 直接讀寫磁盤扇區,現代保護模式 OS 禁止用戶態調用,僅限實模式/DOS 環境。
#include // POSIX
#include // Windows
int access(const char *filename, int mode);
// mode: F_OK(存在), R_OK(讀), W_OK(寫), X_OK(執行)。返回 0 成功,-1 失敗置 errno。
int file_exists(const char *name) { return access(name, F_OK) == 0; }
#include
double acos(double x);
// 定義域 [-1,1],返回值 [0, π] 弧度。
int main() {
printf("acos(0.5) = %f rad = %f deg\n", acos(0.5), acos(0.5)*180/M_PI);
}
#include
int allocmem(unsigned size, unsigned *segp);
// size 以段落(16字節)為單位。現代 OS 不適用。
#include
void far arc(int x, int y, int stangle, int endangle, int radius);
// 以 (x,y) 為圓心,半徑 radius,從 stangle 到 endangle 繪弧(度數,逆時針)。
// 需 initgraph 初始化圖形模式。
#include
char *asctime(const struct tm *tptr);
// 格式固定:"Www Mmm dd hh:mm:ss yyyy\n\0",返回靜態緩衝區指針(非線程安全)。
int main() {
time_t t = time(NULL);
printf("%s", asctime(localtime(&t)));
}
#include
double asin(double x);
// 定義域 [-1,1],返回值 [-π/2, π/2]。
#include
void assert(int expression);
// expression 為 0 時輸出錯誤信息並調用 abort()。定義 NDEBUG 宏可禁用所有 assert。
void add(int *p) { assert(p != NULL); /* ... */ }
#include
double atan(double x);
// 返回值 [-π/2, π/2]。
#include
double atan2(double y, double x);
// 返回值 [-π, π],自動處理象限,x=0 時不會除零。
// 極坐標角度計算首選:angle = atan2(y, x);
#include
int atexit(void (*func)(void));
// 程序正常退出時(exit/return from main)按註冊逆序調用。最多註冊 32/64 個(實現定義)。
void bye1() { puts("bye1"); }
void bye2() { puts("bye2"); }
int main() { atexit(bye1); atexit(bye2); return 0; }
// 輸出: bye2\nbye1\n
#include
double atof(const char *nptr);
// 等價於 strtod(nptr, NULL)。無錯誤檢查,轉換失敗返回 0.0。
#include
int atoi(const char *nptr);
// 等價於 (int)strtol(nptr, NULL, 10)。溢出行為未定義,無錯誤檢查。
// 推薦用 strtol/strtoll 替代。
#include
long atol(const char *nptr);
// 等價於 strtol(nptr, NULL, 10)。同樣無錯誤檢查。
strtol/strtod/strtoul 等,提供完整錯誤檢查(endptr、errno)。-lm (GCC/Clang) 鏈接數學庫。arc、allocmem、absread 僅供歷史代碼維護參考,現代開發請用 SDL/OpenGL/Vulkan、malloc/mmap、文件 I/O 替代。| 函數 | 功能分類 | 頭文件 | 標準 |
|---|---|---|---|
| abort | 進程控制 | stdlib.h | C89 |
| abs | 數學 | stdlib.h | C89 |
| absread/write | 底層磁盤 (DOS) | bios.h/dos.h | 非標準 |
| access | 文件系統 | unistd.h/io.h | POSIX/Win |
| acos/asin/atan/atan2 | 三角反函數 | math.h | C89 |
| allocmem | 內存管理 (DOS) | dos.h | 非標準 |
| arc | 圖形 (BGI) | graphics.h | 非標準 |
| asctime | 時間格式化 | time.h | C89 |
| assert | 調試斷言 | assert.h | C89 |
| atexit | 進程退出鉤子 | stdlib.h | C89 |
| atof/atoi/atol | 字符串轉數值 | stdlib.h | C89 |