English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C library function isdigit() usage and example

C Standard Library <ctype.h>

isdigit() function checks if a character is a digit character (0-9)。

isdigit() function prototype

int isdigit(int arg);

The function isdigit() accepts a single parameter in integer form and returns a value of type int.

Even if isdigit() takes an integer as a parameter, the character is passed to the function. Internally, the character is converted to its ASCII value for checking.

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

C isdigit() return value

Return valueDescription
Non-zero integer (x > 0)

The parameter is a digit character.

0

The parameter is not a digit character.

Example: C isdigit() function

#include <stdio.h>
#include <ctype.h>
int main()
{
    char c;
    c='5';
    printf("Result when passing a digit character: %d", isdigit(c));
    c='+';
    printf("\nResult when passing a non-digit character: %d", isdigit(c));
    return 0;
}

Output Result

Result when passing a digit character: 1
Result when passing a non-digit character: 0

Example: C program to check if a user input character is a digit character

#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 number.", c);
    else
         printf("%c is a number.", c);
    return 0;
}

Output Result

Enter a character: 8
8It is a number.

C Standard Library <ctype.h>