Character classification and conversion
suggest change<ctype.h> declares functions that classify and convert single-byte characters.
Classification functions include:
| Function | Tests for |
|---|---|
isalnum |
letter or decimal digit |
isalpha |
letter |
isblank (C99) |
locale-specific blank character |
iscntrl |
control character |
isdigit |
decimal digit |
isgraph |
printing character other than space |
islower |
lowercase letter |
isprint |
printing character, including space |
ispunct |
punctuation |
isspace |
white-space character |
isupper |
uppercase letter |
isxdigit |
hexadecimal digit |
Each returns zero for false and a nonzero value for true. Except for a few classifications fixed by the standard, results can depend on the current C locale.
The argument must equal EOF or be representable as unsigned char. Passing a negative plain char value directly can cause undefined behavior:
unsigned char value = (unsigned char)*cursor;
if (isalpha(value)) {
/* value is alphabetic in the current locale. */
}
fgetc already returns either EOF or an unsigned char value converted to int, so its result can be passed directly after checking or preserving EOF.
tolower and toupper have the same argument restriction. They return the converted value when a mapping exists and otherwise return the original value.
These functions operate on individual bytes, not general Unicode code points or grapheme clusters. Use an appropriate Unicode library when text processing requires Unicode semantics.