isxdigit() Function

The isxdigit() function in C checks if a given character is a hexadecimal digit. It is useful for validating characters in contexts where hexadecimal input is expected, ensuring that only valid hex digits (0-9, a-f, A-F) are accepted.


Syntax of isxdigit()

</>
Copy
int isxdigit(int c);

Parameters

ParameterDescription
cAn integer representing the character to be checked, typically cast from a char or EOF.

It is important to remember that hexadecimal digits include the numeric characters 0 through 9 and the alphabetic characters a through f (in both lowercase and uppercase). This function ensures that the input character meets these criteria.

Return Value

The function returns a nonzero value (true) if the character is a hexadecimal digit, and zero (false) otherwise.


Examples for isxdigit()

Example 1: Basic Check for a Hexadecimal Digit

This example demonstrates a simple usage of isxdigit() by verifying a single character.

Program

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

int main() {
    int ch = 'B';

    if (isxdigit(ch)) {
        printf("The character %c is a hexadecimal digit.\n", ch);
    } else {
        printf("The character %c is not a hexadecimal digit.\n", ch);
    }

    return 0;
}

Explanation:

  1. A character is set to 'B'.
  2. The isxdigit() function checks if 'B' qualifies as a hexadecimal digit.
  3. Since 'B' is a valid hexadecimal digit, the program prints a confirmation message.

Program Output:

The character B is a hexadecimal digit.

Example 2: Differentiating Between Hexadecimal and Non-Hexadecimal Characters

This example checks several characters to differentiate between hexadecimal digits and other characters.

Program

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

int main() {
    char testChars[] = "9Gf#";
    int i = 0;

    while (testChars[i] != '\0') {
        if (isxdigit(testChars[i])) {
            printf("'%c' is a hexadecimal digit.\n", testChars[i]);
        } else {
            printf("'%c' is not a hexadecimal digit.\n", testChars[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A string "9Gf#" is initialized containing both valid and invalid hexadecimal characters.
  2. The program iterates over each character of the string.
  3. The isxdigit() function checks each character to determine if it is a valid hexadecimal digit.
  4. A message is printed indicating whether each character is a hexadecimal digit.

Program Output:

'9' is a hexadecimal digit.
'G' is not a hexadecimal digit.
'f' is a hexadecimal digit.
'#' is not a hexadecimal digit.

Example 3: Validating a String for Hexadecimal Characters

This example demonstrates how to scan an entire string and validate each character as a hexadecimal digit using isxdigit().

Program

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

int main() {
    char hexStr[] = "1A3Fz";
    int i = 0;

    while (hexStr[i] != '\0') {
        if (isxdigit(hexStr[i])) {
            printf("Character '%c' is a hexadecimal digit.\n", hexStr[i]);
        } else {
            printf("Character '%c' is not a hexadecimal digit.\n", hexStr[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A string "1A3Fz" is defined, containing both valid hexadecimal digits and an invalid character.
  2. The program iterates through each character in the string.
  3. The isxdigit() function validates each character.
  4. For each character, a message is printed to indicate whether it is a valid hexadecimal digit.

Program Output:

Character '1' is a hexadecimal digit.
Character 'A' is a hexadecimal digit.
Character '3' is a hexadecimal digit.
Character 'F' is a hexadecimal digit.
Character 'z' is not a hexadecimal digit.