isalnum() Function Example in C Programming

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

isalnum() Function Overview

The isalnum() function is a member of the <ctype.h> library in C, checks if the given character is an alphanumeric character. An alphanumeric character is defined as either a digit (0-9) or an uppercase or lowercase letter (A-Z, a-z). 

Key Points: 

- The function requires the inclusion of the <ctype.h> header. 

- It takes an int (usually representing a character) as its argument and returns a non-zero value (true) if the character is alphanumeric; otherwise, it returns 0 (false).

Source Code Example

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

int main() {
    char ch;

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

    if (isalnum(ch))
        printf("%c is an alphanumeric character.\n", ch);
    else
        printf("%c is not an alphanumeric character.\n", ch);

    return 0;
}

Output

Enter a character: A
A is an alphanumeric character.

Explanation

1. The required header files, stdio.h for input/output functions and ctype.h for character-related functions are included.

2. Inside the main() function, a character variable ch is declared.

3. The program then prompts the user to input a character.

4. The isalnum() function checks if the entered character is alphanumeric.

5. Depending on the result, a corresponding message is printed to the console.


Comments