w3resource

C isdigit() function

C isdigit(int ch)

The isdigit() function is used to check whether a character is numeric character (0-9) or not. The function is defined in the ctype.h header file.

Numerical digit: A numerical digit (often shortened to just digit) is a single symbol used alone (such as "2") or in combinations (such as "25"), to represent numbers in a positional numeral system. The name "digit" comes from the fact that the ten digits (Latin digiti meaning fingers) of the hands correspond to the ten symbols of the common base 10 numeral system, i.e. the decimal (ancient Latin adjective decem meaning ten) digits.

Function Prototype of isdigit()

int isdigit( int arg );

isdigit() Parameters:

Name Description Required /Optional
ch ch is a character of class digit in the current locale. Required

Return value from isdigit()

  • The isdigit() function returns non-zero if ch is a decimal digit; otherwise, returns 0.

Example: C isdigit() function

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

int main()
{
    char ch;
    ch = '5';
    printf("\nIf %c is  digit character or not? %d", ch, isdigit(ch));
    ch='+';
    printf("\nIf %c is  digit character or not? %d", ch, isdigit(ch));
    ch = 'f';
    printf("\nIf %c is  digit character or not? %d", ch, isdigit(ch));
    return 0;
}

Output:

If 5 is  digit character or not? 1
If + is  digit character or not? 0
If f is  digit character or not? 0

Example: C Program to check a input character 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:

Input a character: 7
7 is a digit.
----------------------- 
Input a character: A
A is not a digit.

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous C Programming: C iscntrl()
Next C Programming: C isgraph()



Follow us on Facebook and Twitter for latest update.