C isxdigit()

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.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges