How to use isxdigit function in C programming?

The isxdigit function in C programming checks whether the argument passed is a hexadecimal-digit character or not. Hexadecimal digits are any of: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F.

It is declared in ctype.h and takes one argument in the form of integer and returns the value of type int. If the character passed is a hexadecimal-digit character, it returns a non-zero integer. If not, it returns 0.

 

Syntax of isxdigit function in C:

//Syntax of isxdigit

int isxdigit(int c);

Parameters:

c => character to classify

Return value:

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

 

Example,

Input : a
Output : Entered character is hexadecimal

Input : 5
Output : Entered character is hexadecimal

Input : @
Output : Entered character is not hexadecimal

 

 

C program to understand the working of isxdigit function:

Consider the below code where I am passing different characters in the isxdigit function. You can see the output of this function with different characters.

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

int main()
{
    unsigned char c = 'A';
    int result = isxdigit(c);
    result ? printf("A is hexadecimal-digit char\n"):printf("A is not a hexadecimal-digit char\n");

    c = '@';
    result = isxdigit(c);
    result ? printf("@ is hexadecimal-digit char\n"):printf("@ is not a hexadecimal-digit char\n");

    c = '\n';
    result = isxdigit(c);
    result ? printf("\\n is hexadecimal-digit char\n"): printf("\\n is not a hexadecimal-digit char\n");

    c = '5';
    result = isxdigit(c);
    result ? printf("5 is hexadecimal-digit char\n"): printf("5 is not a hexadecimal-digit char\n");
    return 0;
}

Output:

isxdigit function in c

 

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

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

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

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

Output1:

Enter a character: 1
1 is a hexadecimal digit character

 

Output2:

Enter a character: a
ais a hexadecimal digit character

 

Output3:

Enter a character: ‘M’
1 is a not hexadecimal digit character

 

C Program to find out total numbers of the hexadecimal Digit present in any given input Using the isxdigit:

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

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

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


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

    unsigned int counter = findCountHexaDecimalDigitsGivenStream(str);

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

    printf("\nNumber of hexadecimal 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 hexadecimal digits in the given input stream is : 7

 

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

As we know the behavior of isxdigit 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_isxdigit(char ch)
{
    return isxdigit((unsigned char)ch);
}

 

Recommended Post:

Leave a Reply

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