C ctype.h
<ctype.h> provides functions for checking what kind of character you have.
It can also convert letters between uppercase and lowercase.
Quick Answer
Use #include <ctype.h> for functions such as isalpha(), isdigit(), isspace(), toupper(), and tolower(). Character-test functions return zero for false and a nonzero value for true. Their arguments must be EOF or values representable as unsigned char.
Include ctype.h
#include <ctype.h>
The header groups two main kinds of tools:
- character classification functions;
- letter case conversion functions.
Character Classification Functions
| Function | Checks For |
|---|---|
isalpha() | Alphabetic character |
isdigit() | Decimal digit |
isalnum() | Letter or digit |
isspace() | Whitespace character |
islower() | Lowercase letter |
isupper() | Uppercase letter |
ispunct() | Punctuation character |
isxdigit() | Hexadecimal digit |
Test Functions Return Zero or Nonzero
if (isalpha('A')) {
printf("It is a letter.\n");
}
Do not assume the true result must be exactly 1.
The standard rule is 0 for false and a nonzero value for true.
isdigit() Example
if (isdigit('7')) {
printf("Digit\n");
}
'7' is a decimal digit, so the condition is true.
isspace() Checks Whitespace
if (isspace('\n')) {
printf("Whitespace\n");
}
Standard whitespace characters include space, tab, newline, vertical tab, form feed, and carriage return.
toupper() and tolower()
int upper = toupper('a');
int lower = tolower('Z');
toupper('a') produces the value for 'A'.
tolower('Z') produces the value for 'z'.
If no conversion applies, the original character value is returned.
C ctype.h Example
#include <stdio.h>
#include <ctype.h>
int main(void) {
int ch = 'a';
if (isalpha(ch)) {
printf("%c is a letter.\n", ch);
}
printf("Uppercase: %c\n", toupper(ch));
printf("isdigit('7'): %s\n",
isdigit('7') ? "true" : "false");
return 0;
}
Output
a is a letter.
Uppercase: A
isdigit('7'): true
The example tests a letter, converts it to uppercase, and checks a decimal digit.
Why These Functions Use int
The character testing and conversion functions use int arguments and return int.
int ch = getchar();
if (ch != EOF && isalpha(ch)) {
/* safe to test */
}
This works naturally with functions such as getchar(), which also return int so they can represent every unsigned-character value plus EOF.
Important: Negative char Values
A ctype function argument must be EOF or a value representable as unsigned char.
char ch = /* some byte */;
if (isalpha((unsigned char)ch)) {
/* safe for ordinary character bytes */
}
On systems where plain char is signed, some byte values become negative when stored in char.
Passing such a negative value directly to a ctype function causes undefined behavior.
Locale Can Affect Character Categories
Character classification and case conversion can depend on the current C locale.
For simple ASCII-style beginner examples such as 'A', 'a', and '7', the expected categories are straightforward.
ctype.h vs Manual Range Checks
if (ch >= 'A' && ch <= 'Z') {
/* manual check */
}
Manual checks can be useful when you specifically want an ASCII-like range.
For general character classification according to the active C locale, the <ctype.h> functions express the intent more clearly.
Common ctype.h Mistakes
| Mistake | Correct Idea |
|---|---|
Checking whether isalpha(ch) == 1. |
Test whether the result is nonzero. |
Passing a negative signed-char value directly. |
Convert ordinary byte data to unsigned char first. |
Passing EOF through an unsigned char cast. |
Keep EOF as int and handle it separately. |
| Assuming case rules can never depend on locale. | Remember that ctype behavior can use the current locale. |
- Which function checks whether a character is a decimal digit?
- What does a nonzero return from
isalpha()mean? - Which function converts a lowercase letter to uppercase?
Practice Answers
1. Which function checks a decimal digit?
Use isdigit().
2. What does nonzero from isalpha() mean?
It means the character is classified as alphabetic in the current locale.
3. Which function converts to uppercase?
Use toupper().
FAQs
What is ctype.h in C?
ctype.h is a standard C header that declares functions for testing character categories and converting letter case.
What does isalpha() return?
isalpha() returns a nonzero value when its argument is an alphabetic character in the current locale, and zero otherwise.
What is the difference between toupper() and tolower()?
toupper() converts a lowercase letter to its uppercase form when a conversion is available, while tolower() converts an uppercase letter to lowercase.
Can any int value be passed to ctype functions?
No. The argument must be EOF or a value representable as unsigned char. Passing another value, such as some negative signed-char values, causes undefined behavior.
Summary
<ctype.h> provides standard tools for testing and converting characters. Functions such as isalpha(), isdigit(), and isspace() return zero or nonzero, while toupper() and tolower() perform case conversion. Always pass EOF or a value representable as unsigned char to these functions.