C Program to find whether the given character is Vowel or Consonant

The five English vowels are: a, e, i, o, u (both upper and lowercase). Every other alphabetic character is a consonant. Non-alphabetic characters (digits, punctuation, spaces) are neither vowel nor consonant. This program uses the <ctype.h> functions isalpha() and tolower() for clean, case-insensitive classification.

The original post used a Gist shortcode ([gist id="7112441"]) that no longer renders — the page showed no code. This rewrite replaces it with a complete program using standard library functions.

C Program: Vowel or Consonant Check

/* Check if a character is a vowel or consonant
 * Vowels: a, e, i, o, u (case-insensitive)
 * Compile: gcc -ansi -Wall -Wextra vowel.c -o vowel */
#include <stdio.h>
#include <ctype.h>

int is_vowel(int c)
{
    int lower = tolower(c);
    return lower == 'a' || lower == 'e' || lower == 'i'
        || lower == 'o' || lower == 'u';
}

int main(void)
{
    int c;
    printf("Enter a character: ");
    c = getchar();
    if (c == EOF || c == '\n') {
        printf("No character entered.\n");
        return 1;
    }

    if (!isalpha(c))
        printf("'%c' is not an alphabetic character.\n", c);
    else if (is_vowel(c))
        printf("'%c' is a Vowel.\n", c);
    else
        printf("'%c' is a Consonant.\n", c);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra vowel.c -o vowel
./vowel

Sample Output

Enter a character: A
'A' is a Vowel.

Enter a character: g
'g' is a Consonant.

Enter a character: E
'E' is a Vowel.

Enter a character: 5
'5' is not an alphabetic character.

Code Explanation

  • tolower(c) before comparing — converts ‘A’ to ‘a’, ‘E’ to ‘e’, etc., so the vowel check is case-insensitive. Without this, you would need to compare against both ‘a’ and ‘A’, ‘e’ and ‘E’, and so on — six conditions per vowel instead of one.
  • isalpha(c) guards before the vowel/consonant check — digits, spaces, and punctuation are neither vowels nor consonants. Checking isalpha(c) first prevents incorrectly reporting, say, ‘5’ as a consonant just because it isn’t in the vowel list.
  • Read with getchar() — reads one character from stdin. Using scanf("%c", &ch) is equivalent for this purpose. getchar() returns int, not char, because it can return EOF (a value that doesn’t fit in unsigned char). Always store the return of getchar() in an int.
  • Why int c not char cgetchar() can return EOF (typically -1) to signal end of input. If c is char and char is signed (as on most platforms), comparing c == EOF after the char range maps -1 correctly — but the comparison is implementation-defined. Storing in int is always correct.
  • Extracting is_vowel() as a function — makes the check reusable. The function takes an int (because ctype.h functions take and return int) and returns 1 for vowel, 0 otherwise — the idiomatic C boolean.

What This Program Teaches

  • ctype.h for character classificationisalpha(), isdigit(), isupper(), islower(), isspace(), isalnum() are more portable and more readable than manual range comparisons like c >= 'A' && c <= 'Z'. Always prefer ctype.h.
  • getchar() returns int, not char — this is one of the most common beginner mistakes in C: char c = getchar() works most of the time but fails to detect EOF correctly on platforms where char is unsigned (EOF is -1, which wraps to 255 as unsigned char, not equal to EOF). Always use int.
  • Case-insensitive comparison with tolower — converting to lowercase before comparing is the standard pattern for case-insensitive character matching. The same technique applies to string comparison: convert both strings to lowercase before calling strcmp.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>