C isxdigit()

The isxdigit() function defined in the ctype.h header file. It helps to check the specified character is a hexadecimal digit(0-9, a-f, A-F) character or not.


int isxdigit( int argument ); #where argument will be a character
 

isxdigit() Parameters:

The isxdigit() function takes a single parameter and is in the form of an integer and the return value should be an integer. When a character is passed it is converted into the integer value corresponding to its ASCII value.

Parameter Description Required / Optional
argument  The character to be checked Required

isxdigit() Return Value

 If the given character is a hexadecimal character, isxdigit() returns a non-zero integer else zero. When the hexadecimal character is passed we will get a different non-zero integer.

Input Return Value
Zero If the parameter is not a hexadecimal character
Non zero number If the parameter is a hexadecimal character

Examples of isxdigit() 

Example 1: How to check the hexadecimal character in C?


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

int main()
{
    char ch;
    ch='5';
    // hexadecimal character
    printf("If a hexadecimal character is passed: %d", isxdigit(ch));

    ch='+';
    // non-hexadecimal character
    printf("\nIf a non-hexadecimal character is passed: %d", isxdigit(ch));

    return 0;
}
 

Output:


If a hexadecimal character is passed: 128
If a non-hexadecimal character is passed: 0

Example 2: How to check character is a hexadecimal or not?


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

int main()
{
    char ch;

    printf("Enter any character: ");
    scanf("%c",&ch);

    if (isxdigit(ch) == 0)
         printf("The given %c is not a hexadecimal.",ch);
    else
         printf("The given %c is a hexadecimal.",ch);
    return 0;
}
 

Output:


Enter any character: 4
The given 4 is a hexadecimal.