C program to find whether a given character is an alphabet,digit or any special character(using ASCII values)

#include<stdio.h>
#include<conio.h>

void main()
{
    char ch;
    clrscr();    //to clear the screen
    printf(“Enter any character:”);
    scanf(“%c”,&ch);

    if((ch>=’A’&&ch<=’Z’)||(ch>=’a’&&ch<=’z’))
        printf(“nYou have entered an alphabet”);
    else
        if(ch>=’0’&&ch<=’9′)
            printf(“nYou have entered a digit”);
        else
            printf(“nYou have entered a special character”);

    getch();    //to stop the screen
}

7 thoughts on “C program to find whether a given character is an alphabet,digit or any special character(using ASCII values)

  1. Nitin

    /* smaller code 🙂 */

    #include

    int main()
    {
    char a;

    printf("Please input a character: ");
    scanf("%c", &a);

    (a >= 'a' && a <= 'z' || a>='A' && a<='Z' || a>='0'&& a<= '9' ? printf("character is not special") : printf("character is special") );

    getch();/* not to use if making prog in command line directly using devc++ */

    }

    Reply
  2. Parikshith Rai

    if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
    what will happen if we don't add parentheses for each expression on either sides of the logical or operator ?!
    pls help 🙂

    Reply
  3. Neeraj Mishra

    We are adding parenthesis to ensure any kind of confusion, otherwise the expression will be executed according to operator precedence and this will cause undesired results. This also make the expression easy to understand. 🙂

    Reply

Leave a Reply

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