In this tutorial, you will learn about C library function isxdigit() which is a character handling function used to check whether the argument supplied is a hexadecimal character or not.

C Library Function isxdigit( )

As discussed earlier, we have to include ctype.h header file for using isxdigit( ) standard character library function.

#include <ctype.h>

C library function isxdigit( ): Function prototype


int isxdigit( int ch);

This function checks whether its argument is a hexadecimal digit character or not. Hexadecimal means A - F , a - f, 0 - 9.

It returns a true value if ch is a hexadecimal digit character and zero (0) otherwise.

Example: Demonstration of isxdigit( ) function

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

int main()
{
   printf("Demonstration of isxdigit():\n");
   printf("\n%s%s", isxdigit('D') ? "D is a " : "D is not a ", "hexadecimal digit");
   printf("\n%s%s", isxdigit('H') ? "H is a " : "H is not a ", "hexadecimal digit");
   printf("\n%s%s", isxdigit('e') ? "e is a " : "e is not a ", "hexadecimal digit");
   printf("\n%s%s", isxdigit('6') ? "6 is a " : "6 is not a ", "hexadecimal digit");
   printf("\n%s%s", isxdigit('!') ? "! is a " : "! is not a ", "hexadecimal digit");

   return 0;
}

[adsense1]

Output

Demonstration of isxdigit():

D is a hexadecimal digit
H is not a hexadecimal digit
e is a hexadecimal digit
6 is a hexadecimal digit
! is not a hexadecimal digit

Explanation
In the above example, standard character library function isxdigit determines whether its argument is a hexadecimal digit or not.

Conditional operator ?: does the selection between two strings "is a" and "is not a".

This can be simply done with the help of if...else statements.

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

int main()
{
   printf("Demonstration of isxdigit( ):\n");

   if( isxdigit('D') == 0)
     printf("\nD is not hexadecimal digit");
   else
     printf("\nD is a hexadecimal digit");

   if( isxdigit('H') == 0)
     printf("\nH is not hexadecimal digit");
   else
     printf("\nH is a hexadecimal digit");

   if( isxdigit('e') == 0)
     printf("\ne is not hexadecimal digit");
   else
     printf("\ne is a hexadecimal digit");

   if( isxdigit('6') == 0)
     printf("\n6 is not hexadecimal digit");
   else
     printf("\n6 is a hexadecimal digit");

   if( isxdigit('!') == 0)
     printf("\n! is not hexadecimal digit");
   else
     printf("\n! is a hexadecimal digit");

   return 0;
}

Output

Demonstration of isxdigit():

D is a hexadecimal digit
H is not a hexadecimal digit
e is a hexadecimal digit
6 is a hexadecimal digit
! is not a hexadecimal digit

Explanation
In this example, we have used if...else statements instead of a conditional operator.

Here, if...else statement does the selection between two strings.