The <ctype.h> header file contains a set of functions used to classify and transform individual characters. These functions are highly efficient because they typically use an internal lookup table based on the ASCII value of the character.
These functions test whether a character belongs to a specific category. They return a non-zero value (True) if the condition is met, and 0 (False) otherwise.
| Function | Checks if Character is... |
|---|---|
isalpha(c) |
An alphabetic letter (a-z, A-Z). |
isdigit(c) |
A decimal digit (0-9). |
isalnum(c) |
Alphanumeric (either a letter or a digit). |
isspace(c) |
A whitespace (space, tab, newline, etc.). |
ispunct(c) |
A punctuation mark. |
These functions check or change the case of a letter. Note that they only affect alphabetic characters; digits and symbols remain unchanged.
Using ctype.h is much cleaner and more portable than manually checking ASCII ranges (like c >= 65 && c <= 90).
You might notice these functions take an int instead of a char. This is to allow them to handle the EOF (End Of File) constant, which is typically -1, ensuring they work correctly during file processing loops.
(unsigned char) to prevent issues with extended ASCII characters or signed-to-unsigned conversion!