w3resource

C isxdigit() function

C isxdigit(int ch)

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

Syntax:

int isxdigit( int arg );

isxdigit() Parameters:

Name Description Required /Optional
ch Argument ch represents a uppercase letter. Required

Return value from isxdigit()

  • The isxdigit() returns non-zero if ch is a hexadecimal digit; otherwise returns 0.

Example-1: C isxdigit() function


#include <stdio.h>
#include <ctype.h>
int main() {
   char ch;
   int result;
   printf("Check whether a character is a hexadecimal digit character or not!\n");
   ch = 'a';
   result = isxdigit(ch);
   printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);
   ch = 'g';
   result = isxdigit(ch);
   printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);
   ch = 'i';
   result = isxdigit(ch);
   printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);
   ch = '0';
   result = isxdigit(ch);
   printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);
   return 0;
}

Output:

Check whether a character is a hexadecimal digit character or not!

Return value is 128 When a is passed as an argemdent.
Return value is 0 When g is passed as an argemdent.
Return value is 0 When i is passed as an argemdent.
Return value is 128 When 0 is passed as an argemdent.

Example-2: Program to Check Hexadecimal Character

#include <stdio.h>
#include <ctype.h>
int main() {
   char ch;
   printf("Input a character: ");
   ch = getchar();

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

Output:

Input a character: b
b is a hexadecimal character.

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous C Programming: C isupper()
Next C Programming: C tolower()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Reading a string with scanf :

An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

Ref : https://bit.ly/3pdEk6f