How to use isdigit function in C programming?

The isdigit function in C programming checks whether the argument passed is a decimal digit character or not. The decimal digits are any of:  0 1 2 3 4 5 6 7 8 9.

It is declared in ctype.h  and takes one argument in the form of integer and returns the value of type int.

 

Syntax of isdigit function in C:

//Syntax of isdigit

int isdigit(int c);

Parameters:

c => character to classify

Return value:

Non-zero value => If the argument is a decimal digit character.
0 => If the argument is neither a decimal digit character.

Example,

Input : a
Output : Zero

Input : 1
Output : Non-zero value

Input : @
Output : Zero

 

C Program to Check whether a Character Entered by User is Decimal Digit or not Using the isdigit:

Let’s see a C program to check given character is a decimal digit or not.

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

int main()
{
    int c;
    printf("Enter a Valid Character: ");
    c = fgetc(stdin);
    if (isdigit(c) == 0)
    {
        printf("%c is not a decimal digit character.", c);
    }
    else
    {
        printf("%c is a decimal digit character.", c);
    }
    return 0;
}

Output1:

Enter a character: 1
1 is a decimal digit character.

Output2:

Enter a character: a
a is not a decimal digit character.

 

There are many applications of isdigit in C programming. But to find out the count of decimal digits in a given input stream is very popular. So let’s see a C code to find the count of the decimal digits characters in the given input stream.

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

unsigned int findCountDecimalDigitsGivenStream(char *str)
{
    unsigned int counter = 0;
    if (str != NULL)
    {
        unsigned int i = 0;
        // counting of alphanumerics
        while (str[i] != '\0')
        {
            if (isdigit((unsigned char)str[i]))
            {
                ++counter;
            }
            ++i;
        }
    }
    // returning total number of decimal-digit
    // present in given input stream
    return (counter);
}


int main()
{
    char str[] = "123_aticleworld";

    unsigned int counter = findCountDecimalDigitsGivenStream(str);

    printf("Total number of char in input stream is : %u\n\n", strlen(str));

    printf("\nNumber of decimal digits in the "
           "given input stream is : %u\n\n", counter);

    return 0;
}

Output:

Total number of char in input stream is : 15

Number of decimal digits in the given input stream is : 3

 

Note: If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of isdigit is undefined.

 

As we know the behavior of isdigit is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.

int my_isdigit(char ch)
{
    return isdigit((unsigned char)ch);
}

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *