C ctype.h Library
C ctype.h Library
In C ctype.h Library provides functions to test and manipulate characters. It’s very useful for validating input, parsing text, and formatting strings.
All functions in ctype.h work with int values representing characters (usually char).
📌 Common Functions in ctype.h
1️⃣ Character Testing Functions
| Function | Description | Example |
|---|---|---|
isalnum(int c) |
Checks if character is alphanumeric (a-z, A-Z, 0-9) | isalnum('A') → true |
isalpha(int c) |
Checks if character is alphabetic | isalpha('1') → false |
isdigit(int c) |
Checks if character is a digit | isdigit('5') → true |
islower(int c) |
Checks if character is lowercase | islower('a') → true |
isupper(int c) |
Checks if character is uppercase | isupper('B') → true |
isspace(int c) |
Checks for whitespace (' ', '\t', '\n') |
isspace(' ') → true |
ispunct(int c) |
Checks if character is punctuation | ispunct('!') → true |
isxdigit(int c) |
Checks if character is hexadecimal digit | isxdigit('F') → true |
isprint(int c) |
Checks if character is printable | isprint('\n') → false |
isgraph(int c) |
Checks if character has graphic representation | isgraph(' ') → false |
2️⃣ Character Conversion Functions
| Function | Description | Example |
|---|---|---|
tolower(int c) |
Converts uppercase to lowercase | tolower('A') → 'a' |
toupper(int c) |
Converts lowercase to uppercase | toupper('b') → 'B' |
3️⃣ Example: Character Validation and Conversion
Sample Output:
4️⃣ Example: Count Character Types in a String
Output:
📌 Summary Table
| Function | Purpose |
|---|---|
isalnum |
Checks alphanumeric |
isalpha |
Checks alphabetic |
isdigit |
Checks digits |
islower |
Checks lowercase letters |
isupper |
Checks uppercase letters |
isspace |
Checks whitespace characters |
ispunct |
Checks punctuation |
isxdigit |
Checks hexadecimal digit |
isprint |
Checks printable characters |
isgraph |
Checks graphic characters |
tolower |
Converts to lowercase |
toupper |
Converts to uppercase |
✅ Notes
-
Functions expect int values, usually casted from
char. -
Always include
#include <ctype.h>. -
Useful for input validation, parsing, and formatting strings.
-
Can be combined with
string.hfor advanced string processing.
