C Program to Check Whether a Character Is an Alphabet
C Program to Check Whether a Character Is an Alphabet
A classroom text exercise should distinguish letter classification from assumptions about character-code numbers.
Use the character-classification library to test a character according to the current C locale.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why alphabetic character uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use the character 7 and trace the selected label.
Alphabetic Character: follow the data
Given data
A classroom text exercise should distinguish letter classification from assumptions about character-code numbers.
Rule
Use the character-classification library to test a character according to the current C locale.
Observed result
Result: alphabetic
Understand the operation
isalpha returns nonzero for a letter recognized in the active locale; it is not a portable promise that every Unicode letter is handled.
ctype functions accept EOF or a value representable as unsigned char. Cast a possibly negative plain char to unsigned char before passing it.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
#include <ctype.h>
int main(void) {
unsigned char letter = 'A';
printf("Result: %s\n", isalpha(letter) ? "alphabetic" : "not alphabetic");
return 0;
}
Expected output
Result: alphabetic
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
#include <ctype.h>
int main(void) {
unsigned char letter = 'A';
printf("Result: %s\n", isalpha(letter) ? "alphabetic" : "not alphabetic");
return 0;
}
The supplied basic character is A.
The letter test is nonzero, selecting the alphabetic label.
Follow the worked calculation
Start with the sample
The supplied basic character is A.
Apply the rule
The letter test is nonzero, selecting the alphabetic label.
Check the result
Result: alphabetic
Result: alphabetic
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <ctype.h>
int main(void) {
unsigned char letter = 'A';
printf("Result: %s\n", isalpha(letter) ? "alphabetic" : "not alphabetic");
return 0;
}
Use the character 7 and trace the selected label.
Worked answer
The changed input produces not alphabetic. The code keeps the valid ctype argument range and does not assume that upper- and lowercase letters have a fixed numeric offset.
C17 · complete program
#include <stdio.h>
#include <ctype.h>
int main(void) {
unsigned char letter = '7';
printf("Result: %s\n", isalpha(letter) ? "alphabetic" : "not alphabetic");
return 0;
}
Expected output
Result: not alphabetic
Run in C compiler →Recognize the important boundaries
This byte-based exercise is not a complete Unicode text classifier. Multibyte encodings need an appropriate decoding and classification design.
Do not pass a negative signed-char value directly to a ctype function.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Use the character-classification library to test a character according to the current C locale.
Continue with bytes and terminated strings.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.