The isdigit() in C programming is a built-in function useful for checking whether a given character is a digit (number) or not. If the given character is between 0 and 9 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), it is a digit.
This program article shows how to check whether a given character is a digit or not using the built-in function Isdigit in C, and without using the function.
C isdigit() syntax
The syntax of the isdigit() function is shown below.
int isdigit (int char);
Parameter: This function accepts a single parameter and checks whether it is a digit or not.
Return Value: The 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 given character is not a digit, it will return 0.
TIP: To use the isdigit() function, we must include the <ctype.h> header file.
How does the C isdigit() function work?
As we all know, the ASCII values of the digits from 0 to 9 are from 48 to 57. When we pass a digit or any other character, the isdigit() function checks whether the ASCII value of the given character is within the range (48 to 57).
It means, if the given character is greater than or equal to 48 or less than or equal to 57, it is a digit, and the isdigit() function returns a non-zero.
C isdigit function Program to Check for Digit
This program allows you to enter any character and check whether the character is a digit or not using the isdigit function available in ctype.h header file.
#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");
}
}

In this isdigit program, we declared a character variable Ch. The below printf statement will ask them to enter any digit they like.
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 the 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");
Checking an Alphabet
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");
For instance, let me enter an alphabet.
Please Enter any alphabet: a
Entered character is Not digit
C Program to Check Whether a Character is a Digit or Not without using Isdigit
As we all know, isdigit is for checking a digit. We can use any of the following approaches.
- (ch >= ‘0’ && ch <= ‘9’)
- (ch >= 48 && ch <= 57)
This program allows the user to enter any character and check whether it is a digit or not without using the isdigit() function.
#include <stdio.h>
int main()
{
char Ch;
printf("Please enter any Character: ");
scanf("%c", &Ch);
if (Ch >= '0' && Ch <= '9')
{
printf ( "Entered character is a digit") ;
}
else
{
printf("Entered character is Not a digit");
}
}
Please enter any Character: 7
Entered character is a digit
Within this C Program to Check Whether the Character is a 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 following statement.
printf ("\n Entered character is a digit");
If the above condition (Ch >= ‘0’ && Ch <= ‘0’) is FALSE, the given character is not a digit. So, it will print the following statement.
printf ("\n Entered character is not a digit");
C Program to Check Whether a Character is a Digit or Not using ASCII Values
This program checks whether the character is a digit or not without using an ASCII code or value. Within this isdigit program, if you look at the If Statement
if (Ch >= 48 && Ch <= 57)
As we all know, the 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 the following statement.
printf ("\n Entered character is a digit");
If the above condition if (Ch >= 48 && Ch <= 57) is FALSE, it is not a digit. So, it will print the following statement.
printf ("\n Entered character is not a digit");
Example
#include <stdio.h>
int main()
{
char Ch;
printf("Please Enter any character: ");
scanf("%c", &Ch);
if (Ch >= 48 && Ch <= 57)
{
printf ("It is a digit") ;
}
else
{
printf("It is Not a digit");
}
}
Please enter any character: 0
It is a digit
Real-time examples of isdigit in C programming
How to Extract Digits from a String?
By default, the isdigit() function does not work on a string directly. However, we can use a for loop or a while loop to iterate over individual characters in a string and apply the isdigit() function on them.
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[] = "Message:2026";
int i = 0;
while (str[i])
{
if (isdigit((unsigned char)str[i]))
{
putchar(str[i]);
}
i++;
}
return 0;
}
2026
Using isdigit() on a string
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[] = "Nq123L4";
for (int i = 0; str[i] != '\0'; i++)
{
if (isdigit((unsigned char)str[i]))
{
printf("%c is a digit\n", str[i]);
}
else
{
printf("%c is NOT a digit\n", str[i]);
}
}
return 0;
}
N is NOT a digit
q is NOT a digit
1 is a digit
2 is a digit
3 is a digit
L is NOT a digit
4 is a digit
Does the C isdigit() function work with negative values?
The isdigit() function does not work with negative characters. If we set the character to -1 or a value greater than 127, it returns undefined values.
char ch = -1; char ch = 190;
The above two are wrong when we apply isdigit(ch). So, it is safer to use
isdigit((unsigned char)ch)
Difference between isdigit, ixdigit(), and isalnum()
- isdigit(): (Digits Only) – Checks whether a character is a digit (0 to 9) or not.
- Isxdigit(): Checks whether a character is a digit (0 to 9), letters between lowercase a to f, or uppercase A to F.
- isalnum(): (Alphabets + Digits)- Checks whether a character is alphanumeric. If it is an uppercase (A to Z), lowercase (a to z) letter, or digits (0 to 9).