C Program to Check Vowel or Consonant
C Program to Check Vowel or Consonant
A classroom text exercise should distinguish letter classification from assumptions about character-code numbers.
Classify a basic English vowel after case normalization; classify other letters as consonants and reject nonletters separately.
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 vowel or consonant uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use the character b and trace the selected label.
Vowel or Consonant: follow the data
Given data
A classroom text exercise should distinguish letter classification from assumptions about character-code numbers.
Rule
Classify a basic English vowel after case normalization; classify other letters as consonants and reject nonletters separately.
Observed result
Result: vowel
Understand the operation
tolower allows A and a to share the vowel test. Check isalpha first so a digit is not mislabeled a consonant.
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';
int lower = tolower(letter);
if (!isalpha(letter)) puts("Result: not a letter");
else printf("Result: %s\n", lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u' ? "vowel" : "consonant");
return 0;
}
Expected output
Result: vowel
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';
int lower = tolower(letter);
if (!isalpha(letter)) puts("Result: not a letter");
else printf("Result: %s\n", lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u' ? "vowel" : "consonant");
return 0;
}
The supplied basic character is A.
Its normalized form is a, which matches the vowel alternatives.
Follow the worked calculation
Start with the sample
The supplied basic character is A.
Apply the rule
Its normalized form is a, which matches the vowel alternatives.
Check the result
Result: vowel
Result: vowel
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <ctype.h>
int main(void) {
unsigned char letter = 'A';
int lower = tolower(letter);
if (!isalpha(letter)) puts("Result: not a letter");
else printf("Result: %s\n", lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u' ? "vowel" : "consonant");
return 0;
}
Use the character b and trace the selected label.
Worked answer
The changed input produces consonant. 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 = 'b';
int lower = tolower(letter);
if (!isalpha(letter)) puts("Result: not a letter");
else printf("Result: %s\n", lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u' ? "vowel" : "consonant");
return 0;
}
Expected output
Result: consonant
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
Classify a basic English vowel after case normalization; classify other letters as consonants and reject nonletters separately.
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.