首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C 標準庫函數速查:以 A 開頭函數 (abort、abs、access、acos、asctime、assert、atan、atexit、atof、atoi 等)

     編輯:關於C語言

C 標準庫函數速查:A 系列

本文匯總 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>字符串轉 doubledouble atof(const char *str);轉換結果
atoi<stdlib.h>字符串轉 intint atoi(const char *str);轉換結果
atol<stdlib.h>字符串轉 longlong 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。
  • DOS 專用函數 (absread/allocmem/arc) 僅適用於 16 位實模式環境,現代開發不建議使用。

小結

A 系列函數涵蓋進程控制、數學運算、時間轉換、斷言、字符串轉換、退出註冊等高頻領域。掌握原型、頭文件、返回值與避坑點,能顯著提升查閱效率與代碼健壯性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved