| 函數 | 原型 | 說明 | 備註 |
|---|---|---|---|
atof | double atof(const char *nptr) | 字串轉 double,支援科學記號 | 等同 strtod(nptr, NULL) |
atoi | int atoi(const char *nptr) | 字串轉 int | 等同 strtol(nptr, NULL, 10) |
atol | long atol(const char *nptr) | 字串轉 long | 等同 strtol(nptr, NULL, 10) |
strtod | double strtod(const char*, char**) | 字串轉 double,可取得結束位置 | 支援進制前綴 |
strtol | long strtol(const char*, char**, int base) | 字串轉 long,base 2~36 或 0(自動) | 溢位設 errno=ERANGE |
strtoul | unsigned long strtoul(...) | 字串轉 unsigned long | 同 strtol 但無符號 |
共同行為:跳過前導空白 → 遇數字/符號開始 → 遇非數字或 \0 結束 → endptr 回傳停止位置。
| 函數 | 原型 | 特點 |
|---|---|---|
gcvt | char *gcvt(double val, int ndigit, char *buf) | 四捨五入,含小數點/符號,結果存 buf |
ecvt | char *ecvt(double val, int ndigit, int *decpt, int *sign) | 不含小數點,decpt 回傳小數點位置 |
fcvt | char *fcvt(double val, int ndigit, int *decpt, int *sign) | 固定小數位數,其餘同 ecvt |
char *itoa(int value, char *str, int radix); // 2~36 進制 char *ltoa(long value, char *str, int radix); char *ultoa(unsigned long value, char *str, int radix);
標準 C 可用 sprintf(buf, "%d", val) 或 snprintf 替代,更安全可攜。
tolower(c) / toupper(c):<ctype.h>,僅影響字母toascii(c):清除第 8 位,轉 7-bit ASCIIatoi 等舊函數無錯誤回報,建議改用 strtol 族並檢查 endptr 与 errnogcvt/ecvt/fcvt 非執行緒安全(靜態緩衝區),多執行緒請用 snprintfitoa 族非標準,跨平台專案請自行封裝或用 sprintf