C isdigit()

The isdigit() function checks whether a character is numeric character (0-9) or not.

Function Prototype of isdigit()

int isdigit( int arg );

Function isdigit() takes a single argument in the form of an integer and returns the value of type int.

Even though, isdigit() takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII value for the check.

It is defined in <ctype.h> header file.


C isdigit() Return value

Return Value Remarks
Non-zero integer ( x > 0 ) Argument is a numeric character.
Zero (0) Argument is not a numeric character.

Example: C isdigit() function

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

int main()
{
    char c;
    c='5';
    printf("Result when numeric character is passed: %d", isdigit(c));

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

    return 0;
}

Output

Result when numeric character is passed: 1
Result when non-numeric character is passed: 0

Example: C Program to Check whether a Character Entered by User is Numeric Character or Not

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

int main()
{
    char c;

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

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

Output

Enter a character: 8
8 is a digit.