Join our newsletter for the latest updates.

C isxdigit()

The isxdigit() function checks whether a character is a hexadecimal digit character (0-9, a-f, A-F) or not.

The function prototype of isxdigit() is:

int isxdigit( int arg );

It is defined in the <ctype.h> header file.


isxdigit() Parameters

The isxdigit() function takes a single character as a parameter.

Note: In C programming, characters are treated as int values internally.


C isxdigit() Return Value

If the argument passed to isxdigit() is

  • a hexadecimal character, isxdigit() returns a non-zero integer.
  • a non-hexadecimal character, isxdigit() returns 0.

Example 1: C isxdigit() function

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

int main() {
   char c = '5';
   int result;

   // hexadecimal character is passed
   result = isxdigit(c); // result is non-zero
   printf("Result when %c is passed to isxdigit(): %d", c, isxdigit(c));

   c = 'M';

   // non-hexadecimal character is passed
   result = isxdigit(c); // result is 0

   printf("\nResult when %c is passed to isxdigit(): %d", c, isxdigit(c));

   return 0;
}

Output

Result when 5 is passed to isxdigit(): 128
Result when M is passed to isxdigit(): 0

Example 2: Program to Check Hexadecimal Character

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

int main() {
   char c = '5';

   printf("Enter a character: ");
   c = getchar();

   if (isxdigit(c) != 0) {
      printf("%c is a hexadecimal character.", c);
   } else {
      printf("%c is not a hexadecimal character.", c);
   }

   return 0;
}

Output

Enter a character: f
f is a hexadecimal character.