C Library Function - isxdigit()
C Standard Library - <ctype.h>
Description
The C library function int isxdigit(int c) checks if the passed character is a hexadecimal digit.
int isxdigit(int c) parameter c is a single character.
Hexadecimal digits are typically represented by the numbers 0 to 9 and the letters A to F (or a to f), where A to F represent 10 to 15: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F.
Declaration
Here is the declaration for the isxdigit() function.
int isxdigit(int c);
Parameters
- c -- This is the character to be checked.
Return Value
If c is a hexadecimal digit, the function returns a non-zero integer value; otherwise, it returns 0.
Example
The following example demonstrates the use of the isxdigit() function.
#include <ctype.h>
#include <stdio.h>
int main() {
char c = '5';
int result;
// Passing the character
result = isxdigit(c); // result returns non-zero
printf("%c passed to isxdigit() function result is: %d", c, isxdigit(c));
printf("\n"); // New line
c = 'M';
// Non-hexadecimal digit as parameter
result = isxdigit(c); // result is 0
printf("%c passed to isxdigit() function result is: %d", c, isxdigit(c));
return 0;
}
Let's compile and run the above program, which will produce the following result:
5 passed to isxdigit() function result is: 1
M passed to isxdigit() function result is: 0
Finding hexadecimal digits in a string:
#include<ctype.h>
#include<stdio.h>
int main()
{
char str[]="123c@#run[oobe?";
int i;
for(i=0;str[i]!='\0';i++) {
if(isxdigit(str[i])) {
printf("%c is a hexadecimal digit\n",str[i]);
}
}
}
Let's compile and run the above program, which will produce the following result:
1 is a hexadecimal digit
2 is a hexadecimal digit
3 is a hexadecimal digit
c is a hexadecimal digit
b is a hexadecimal digit
e is a hexadecimal digit