C isdigit()

The isdigit() function defined in the ctype.h header file. It helps to check the specified character is a decimal digit or a numeric(0-9).


int isdigit( int argument ); #where argument will be a character
 

isdigit() Parameters:

The isdigit() function takes a single parameter and is in the form of an integer and the return value should be an integer. When a character is passed it is converted into the integer value corresponding to its ASCII value.

Parameter Description Required / Optional
argument  The character to be checked Required

isdigit() Return Value

 If the given character is a numeric character, isdigit() returns a non-zero integer else zero. When the numeric character is passed we will get a different non-zero integer.

Input Return Value
Zero If the parameter is not a numeric character
Non zero number If the parameter is a numeric character

Examples of isdigit() method in Python

Example 1: How to check numeric character in C?


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

int main()
{
    char ch;
    ch='4';
    printf("If numeric character is passed: %d", isdigit(ch));

    ch='+';
    printf("\nIf non-numeric character is passed: %d", isdigit(ch));

    return 0;
}
 

Output:


If numeric character is passed: 1
If non-numeric character is passed: 0

Example 2: How to check character is numeric or not?


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

int main()
{
    char ch;

    printf("Enter any character: ");
    scanf("%c",&ch);

    if (isdigit(ch) == 0)
         printf("The given %c is not a digit.",ch);
    else
         printf("The given %c is a digit.",ch);
    return 0;
}
 

Output:


Enter any character: 4
The given 4 is a digit.