| 函數 | 判斷條件 | 示例 |
|---|---|---|
isalpha(c) | 字母 A-Z, a-z | isalpha('c')=真 |
isdigit(c) | 數字 0-9 | isdigit('1')=真 |
isxdigit(c) | 十六進制 0-9, A-F, a-f | isxdigit('A')=真 |
isalnum(c) | 字母或數字 | isalnum('+')=假 |
islower(c) | 小寫字母 | |
isupper(c) | 大寫字母 | |
isspace(c) | 空白字符:空格 \t \n \r \v \f | |
ispunct(c) | 標點符號(非字母數字空白) | |
iscntrl(c) | 控制字符 | |
isprint(c) | 可打印字符(含空格) | |
isgraph(c) | 可打印非空格字符 |
| 函數 | 功能 | 返回值 |
|---|---|---|
tolower(c) | 大寫轉小寫 | 轉換後字符(非字母不變) |
toupper(c) | 小寫轉大寫 | 轉換後字符 |
unsigned char值或EOF,直接傳char可能負值導致未定義行為isalpha(c++)危險)setlocale(LC_CTYPE, "")後判斷依賴當前區域輸出:c is alpha + is other + is other 1 is digit 1 is digit實戰示例 2:十六進制字符串轉長整型
實戰示例 3:字符串大小寫轉換
總結
ctype.h提供標準、可移植的字符分類與轉換。掌握參數int規範、宏副作用陷阱、locale敏感性,能安全高效處理文本解析、驗證、轉換任務。isxdigit+toupper+strchr組合是解析十六進制/標識符的經典模式。