When handling character input in C, one of the most useful tasks is checking what type of character was entered. This article explains how to write a C program character type checker that detects whether the input is a capital letter, small letter, digit, or special character.
Why Use a C Program Character Type Checker?
Character classification is essential when building programs that involve user input validation or text processing. By using ASCII ranges and simple conditions, a C program character type checker helps maintain clean input and prevent errors.
ASCII Ranges to Classify Characters
C language relies on ASCII values to represent characters. Here’s a quick table of ranges:
Character Type | ASCII Range |
---|---|
Capital Letters | 65–90 |
Small Letters | 97–122 |
Digits | 48–57 |
Characters outside these ranges are classified as special characters. For a full list, visit the ASCII Table.
C Program to Identify Character Type
#include <stdio.h>
int main() {
char ch;
// Input character from user
printf("Enter any character: ");
scanf("%c", &ch);
// Check and classify
if (ch >= 'A' && ch <= 'Z') {
printf("It is a Capital Letter.\n");
}
else if (ch >= 'a' && ch <= 'z') {
printf("It is a Small Letter.\n");
}
else if (ch >= '0' && ch <= '9') {
printf("It is a Digit.\n");
}
else {
printf("It is a Special Character.\n");
}
return 0;
}
Explanation of the Code
This simple program takes a character as input and checks its ASCII value using conditional statements. If the character falls between:
A-Z
→ Capital lettera-z
→ Small letter0-9
→ Digit- Else → Special character
Each condition is straightforward and avoids the need for additional libraries.
Example Output
Enter any character: 9
It is a Digit.
Enter any character: B
It is a Capital Letter.
Enter any character: @
It is a Special Character.
Conclusion
Using ASCII values and condition checks, this C program character type logic helps programmers accurately classify user input. It’s a great way to practice conditional logic and input handling in C.