In C programming language you can find multiple library functions that allow you to determine a type of a
character. Today we are going to discuss one of such functions, namely the isxdigit function. In this tutorial you can expect to find:
Return: non-zero value if int c is a hexadecimal ASCII value. Otherwise, 0.
Parameter list: (int
c) - the character that is checked to determine the return value.
Header file:<ctype.h>.
1. So, what is isxdigit in C?
The isxdigit function helps programmers to determine if a
given character contains a hexadecimal value. All digit values ([0-9]), as well as, first six
letters of uppercase and lowercase alphabets ([A-F] and [a-f]) are hexadecimal.
These values are represented in the ASCII table. The digits are represented in ASCII encoding system from 48 to
57, first six uppercase character range from 65 to 70 and lowercase characters range from 97 to 102.
Now, if the value received by the isxdigit function falls on
one of these ranges, it is considered a hexadecimal value and a non-zero integer is returned. Otherwise,
0 is returned.
Example 1: Usage of isxdigit.
#include<stdio.h>
#include<ctype.h>
intmain(void)
{
printf("%d\n",isxdigit('a'));
printf("%d\n",isxdigit('1'));
return(0);
}
Possible output:
1 1
2. And how to rewrite the isxdigit function in C?
To write an implementation of the isxdigit function, we need to
check if the received parameter is within one of the hexadecimal ranges. If so, we need to return a
non-zero integer value. In case of the received parameter not being found within one of the hexadecimal
ranges, we should return a 0. In the code snippet below you can find one way as to how achieve this.
Example 2: Rewritten isxdigit function.
intour_isxdigit ( int c )
{
if((c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 &&
c <=
102))
return( 1 );
return( 0 );
}
What you can see in the conditional statement (on line 3), is a condition that check the
received parameter to determine if it falls on one of the hexadecimal ranges. Given that it is the case,
then this implementation of the isxdigit returns 1. Otherwise,
return value is set to 0. And so the isxdigit function is
rewritten and implemented.
Congratulations! You have reached the last part of this tutorial. Now you are ready to take the quiz!