isxdigit() Function Example in C Programming

In this source code example, we will see how to use the isxdigit() function in C programming with an example.

isxdigit() Function Overview

The isxdigit() function is a part of the <ctype.h> library in C. It checks if the provided character represents a hexadecimal digit (0-9, A-F, a-f). 

 Key Points: 

- The <ctype.h> header must be included to utilize the function. 

- The function accepts an int, which is typically the ASCII value of a character. 

- It returns a non-zero value (true) if the character is a hexadecimal digit and zero (false) otherwise.

Source Code Example

#include <stdio.h>
#include <ctype.h>  // Required for isxdigit()

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

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

    return 0;
}

Output

Enter a character: A
The character you entered is a hexadecimal digit.
Or
Enter a character: Z
The character you entered is not a hexadecimal digit.

Explanation

1. The program starts by including the necessary header files: stdio.h for standard I/O operations and ctype.h for the isxdigit() function.

2. The main() function begins with the declaration of the character variable ch.

3. The user is then prompted to input a character, which gets stored in the ch variable.

4. The isxdigit() function assesses whether the character represents a hexadecimal digit.

5. Depending on the outcome, an appropriate message is displayed to the user, indicating the character's status as a hexadecimal digit or not.


Comments