IsDigit in C Programming

How to check whether the given character is a digit or not using the built-in function IsDigit in C and not using the 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.

C isdigit function SYntax

The syntax of the C isdigit function is shown below.

isdigit (<Character>);

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

  • If the character inside the isdigit function is a digit, 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 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 C isdigit program, we declared a character variable Ch. The below printf statement will ask them to enter any digit he likes.

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

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

scanf("%c", &Ch);

In the next line, we used the C language If Statement. Please refer to the 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 one 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, it is not a digit. So, it will print this statement.

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

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

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

#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 the Character is Digit or Not example, if you look at the If Statement.

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

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

The given character is a digit if the above condition (Ch >= ‘0’ && Ch <= ‘0’) is TRUE. So, it will print the 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 the 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 program, If you look at the If Statement

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

As we all know, ASCII values of all the digits 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, it is not a digit. So, it will print the below statement.

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