IsDigit in C Programming

How to check whether the given character is a digit or not using built-in function IsDigit in C and not using isdigit function. The C Programming isdigit is a built-in function present in the header file, which is helpful to check whether the character is a digit or not. The Syntax of the C isdigit function is

isdigit (<Character>);

The C isdigit function will return an integer value as output.

  • If the character inside the isdigit function is a digit, then it will return non zero value
  • If the character inside the isdigit function is not a digit, it will return 0

Program to Check for Digit using IsDigit in C Programming

This isdigit in C program allows you to enter any character and check whether the character is a digit or not using isdigit function available in ctype.h header file.

/* C Program to check whether the character is Digit or not */

#include <stdio.h> 
#include <ctype.h>

int main()
{
  char Ch;
 
  printf("\n Please Enter any alphabet\n");
  scanf("%c", &Ch);

  if ( isdigit(Ch) )
   {  
     printf ("\n Entered character is digit");
   }
  else
   {
     printf("\n Entered character is Not digit");
   }  
}
Check Whether Character is Digit or Not using isdigit in C

In this isdigit in c program, we declared a character variable Ch. The below printf statement will ask to enter any character he likes.

printf("\n Please Enter any character \n");

Below scanf statement will assign the user entered character to Ch variable

scanf("%c", &Ch);

In the next line, we used the C language If Statement. Please refer to If Statement article to understand the If condition functionality. Within this, we used C isdigit function

if (isdigit(Ch))

If the above isdigit(Ch) condition is TRUE, the given character is a digit. So, this C program will print the below statement

printf ("\n Entered character is digit");

If the above isDigit condition is FALSE, the given character is not a digit. So, it will print this statement.

printf ("\n Entered character is Not digit");

C Program to Check Whether Character is Digit or Not without using Isdigit in C

This C program allows the user to enter any character and check whether the character is a digit or not without using isdigit

/*C Program to check whether the character is Digit or not */
#include <stdio.h> 

int main()
{
  char Ch;

  printf("\n Please Enter any alphabet\n");
  scanf("%c", &Ch);

  if (Ch >= '0' && Ch <= '9')
   {  
     printf ( "\n Entered character is digit") ;
   }
  else
   {
     printf("\n Entered character is Not digit");
   }  
}
 Please Enter any alphabet
7

 Entered character is digit

Within this C Program to Check Whether Character is Digit or Not example if you look at the If Statement

if (Ch >= '0' && Ch <= '0')

As we all know that, all the digit characters are between 0 and 9. So, the above if condition will check whether the given character is between 0 and 9.

If the above condition (Ch >= ‘0’ && Ch <= ‘0’) is TRUE, the given character is a digit. So, it will print below statement

printf ("\n Entered character is digit");

If the above condition (Ch >= ‘0’ && Ch <= ‘0’) is FALSE, the given character is not a digit. So, it will print below statement

printf ("\n Entered character is Not digit");

C Program to Check Whether Character is Digit or Not using ASCII Values

This C program checks whether the character is a digit or not without using ASCII value.

#include <stdio.h> 

int main()
{
  char Ch;
  
  printf("\n Please Enter any character\n");
  scanf("%c", &Ch);

  if (Ch >= 48 && Ch <= 57)
   {  
     printf ( "\n Entered character is digit") ;
   }
  else
   {
     printf("\n Entered character is Not digit");
   }  
}
 Please Enter any character
0

 Entered character is digit

Within this isdigit in c program, If you look at the If Statement

if (Ch >= 48 && Ch <= 57)

As we all know that, ASCII values of all the digit characters are between 48 and 57. So, the above if condition will check whether the given character is between 48 and 57.

If the above condition if (Ch >= 48 && Ch <= 57) is TRUE, the given character is a digit, So, it will print below statement

printf ("\n Entered character is digit");

If the above condition if (Ch >= 48 && Ch <= 57) is FALSE, the given character is not a digit. So, it will print below statement

printf ("\n Entered character is Not digit");