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.

  1. Explain

    Explain why vowel or consonant uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use the character b and trace the selected label.

Vowel or Consonant: follow the data

  1. Given data

    A classroom text exercise should distinguish letter classification from assumptions about character-code numbers.

  2. Rule

    Classify a basic English vowel after case normalization; classify other letters as consonants and reject nonletters separately.

  3. Observed result

    Result: vowel

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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

  1. Start with the sample

    The supplied basic character is A.

  2. Apply the rule

    Its normalized form is a, which matches the vowel alternatives.

  3. Check the result

    Result: vowel

    Result: vowel
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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.

Why represent or cast the argument as unsigned char?
Choose one answer. You can try again.

Answer and explanation

The classification function requires a valid unsigned-char value or EOF.

Correct. A negative ordinary char can violate its argument contract.

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.

    Next lesson: C Program to Check Whether a Character Is an Alphabet →