Check input character is alphabet, digit or special character in C


In this section, we will see how to check whether a given character is number, or the alphabet or some special character in C.

The alphabets are from A – Z and a – z, Then the numbers are from 0 – 9. And all other characters are special characters. So If we check the conditions using these criteria, we can easily find them.

Example

#include <stdio.h>
#include <conio.h>
main() {
   char ch;
   printf("Enter a character: ");
   scanf("%c", &ch);
   if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
      printf("This is an alphabet");
   else if(ch >= '0' && ch <= '9')
      printf("This is a number");
   else
      printf("This is a special character");
}

Output

Enter a character: F
This is an alphabet

Output

Enter a character: r
This is an alphabet

Output

Enter a character: 7
This is a number

Output

Enter a character: #
This is a special character

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements