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.

C Programming: Tips of the Day

Reading a string with scanf :

An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

Ref : https://bit.ly/3pdEk6f