涵蓋程式控制、數學運算、字串轉換、檔案存取、時間日期、斷言、錯誤處理等核心函數,適合開發時快速查閱。
| 函數 | 原型 | 功能 | 範例 |
|---|---|---|---|
| abort | void abort(void) | 異常終止程式 (不呼叫 atexit) | abort(); |
| atexit | int atexit(void (*func)(void)) | 註冊正常結束時呼叫的函數 | atexit(cleanup); |
| exit | void exit(int status) | 正常終止 (呼叫 atexit、刷新緩衝區) | exit(EXIT_SUCCESS); |
| system | int system(const char *cmd) | 執行殼層命令 | system("pause"); |
| assert | void assert(int expr) | 除錯斷言,失敗終止並顯示資訊 | assert(ptr != NULL); |
| 函數 | 原型 | 功能 | 備註 |
|---|---|---|---|
| abs | int abs(int n) | 整數絕對值 | |
| labs/llabs | long/long long 版本 | 長整數絕對值 | |
| fabs | double fabs(double x) | 浮點數絕對值 | |
| sqrt | double sqrt(double x) | 平方根 | x >= 0 |
| pow | double pow(double x, double y) | x 的 y 次方 | |
| sin/cos/tan | double sin(double x) | 三角函數 (弧度) | |
| asin/acos/atan | 反三角函數 | 回傳弧度 [-π/2, π/2] | |
| atan2 | double atan2(double y, double x) | y/x 反正切,處理象限 | 優於 atan(y/x) |
| exp/log | 指數/自然對數 | e^x, ln(x) | |
| ceil/floor | 向上/向下取整 | 回傳 double | |
| fmod | 浮點數餘數 | x - n*y |
| 函數 | 原型 | 功能 | 錯誤處理 |
|---|---|---|---|
| atof | double atof(const char *s) | 字串轉 double | 無錯誤回傳,不可檢測 |
| atoi | int atoi(const char *s) | 字串轉 int | 同上 |
| atol/atoll | long/long long 轉換 | 字串轉長整數 | 同上 |
| strtod | double strtod(const char*, char**) | 字串轉 double | 可檢測錯誤、支援 16 進位 |
| strtol/strtoll | 長整數轉換 | 字串轉 long/long long | 可指定基數、檢測錯誤 |
| strtoul/strtoull | 無號長整數轉換 | 字串轉 unsigned long | 同上 |
建議:新代碼優先使用 strto* 族函數,提供完整錯誤檢測。
| 函數 | 功能 | 函數 | 功能 |
|---|---|---|---|
| isalnum | 字母數字 | tolower | 轉小寫 |
| isalpha | 字母 | toupper | 轉大寫 |
| isdigit | 數字 | isspace | 空白字元 |
| isxdigit | 16進位數字 | iscntrl | 控制字元 |
| isprint | 可列印字元 | ispunct | 標點符號 |
注意:參數為 int,值需為 unsigned char 或 EOF,否則 UB。
| 函數 | 原型 | 功能 |
|---|---|---|
| memcpy | void* memcpy(void* dest, const void* src, size_t n) | 複製 n 位元組 (不檢查重疊) |
| memmove | void* memmove(void* dest, const void* src, size_t n) | 複製 n 位元組 (安全處理重疊) |
| memset | void* memset(void* s, int c, size_t n) | 設定 n 位元組為字元 c |
| memcmp | int memcmp(const void* s1, const void* s2, size_t n) | 比較 n 位元組 |
| malloc | void* malloc(size_t size) | 分配未初始化記憶體 |
| calloc | void* calloc(size_t n, size_t size) | 分配並歸零 |
| realloc | void* realloc(void* ptr, size_t size) | 調整大小 (可能移動) |
| free | void free(void* ptr) | 釋放記憶體 |
| 函數 | 原型 | 功能 |
|---|---|---|
| fopen | FILE* fopen(const char* path, const char* mode) | 開啟檔案 |
| fclose | int fclose(FILE* fp) | 關閉檔案 |
| fgetc/fputc | int fgetc(FILE*) / int fputc(int c, FILE*) | 字元讀/寫 |
| fgets/fputs | 字串讀/寫 | 逐行處理 |
| fread/fwrite | size_t fread(void*, size_t, size_t, FILE*) | 區塊二進位讀/寫 |
| fprintf/fscanf | 格式化讀/寫 | 結構化文字 |
| fseek/ftell | 隨機存取定位 | 位元組偏移 |
| rewind | void rewind(FILE*) | 定位到檔案開頭 |
| feof/ferror | 檢測結尾/錯誤 | 狀態查詢 |
| remove/rename | 刪除/重命名檔案 | 檔案管理 |
| 函數/類型 | 功能 |
|---|---|
| time_t | 自 1970-01-01 秒數 |
| struct tm | 分解時間 (年月日時分秒) |
| time() | 取得當前時間 |
| localtime/gmtime | 轉換為本地/UTC 時間 |
| asctime/ctime | 轉換為字串 |
| strftime | 格式化輸出時間字串 |
| difftime | 兩時間差 (秒) |
| clock | 程式 CPU 時間 |
abortexitatexitassertabs/fabs/labssqrtpowsin/cos/tan/asin/acos/atan/atan2strtod/strtol/strtoul (推薦) / atof/atoi/atol (簡易)memcpy/memmovememsetmalloc/calloc/realloc/freefopen (模式: r/w/a/r+/w+/a+)strftime