isalnum() Function

The isalnum() function in C determines if a character is alphanumeric, meaning it checks if the character is a digit or a letter. This function is widely used to validate characters in data processing and parsing operations.


Syntax of isalnum()

</>
Copy
int isalnum(int c);

Parameters

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

It is important to note that what constitutes a letter might vary based on the locale in use. In the default “C” locale, the function considers a character a letter if either isupper() or islower() would return true.

Return Value

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


Examples for isalnum()

Example 1: Basic Check for Alphanumeric Character

This example demonstrates the basic usage of isalnum() by checking a single character.

Program

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

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

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

    return 0;
}

Explanation:

  1. A character is assigned the value 'A'.
  2. The isalnum() function checks if 'A' is alphanumeric.
  3. Since 'A' is a letter, the condition is true and the program prints a confirmation message.

Program Output:

The character A is alphanumeric.

Example 2: Differentiating Between Alphanumeric and Non-Alphanumeric Characters

This example checks multiple characters to differentiate between alphanumeric and non-alphanumeric types.

Program

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

int main() {
    char testChars[] = "aB3$";
    int i = 0;

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

    return 0;
}

Explanation:

  1. A string "aB3$" is initialized containing both alphanumeric and non-alphanumeric characters.
  2. The program iterates through each character using a while loop.
  3. For each character, isalnum() determines if it is alphanumeric, and the appropriate message is printed.

Program Output:

'a' is alphanumeric.
'B' is alphanumeric.
'3' is alphanumeric.
'$' is not alphanumeric.

Example 3: Checking Each Character in a String for Alphanumeric Content

This example demonstrates how to scan an entire string and use isalnum() to validate each character.

Program

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

int main() {
    char str[] = "Hello123!";
    int i = 0;

    while (str[i] != '\0') {
        if (isalnum(str[i])) {
            printf("Character '%c' is alphanumeric.\n", str[i]);
        } else {
            printf("Character '%c' is not alphanumeric.\n", str[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A string "Hello123!" is defined.
  2. The program iterates over each character in the string.
  3. The isalnum() function is used to check whether each character is alphanumeric.
  4. A message is printed for each character indicating whether it is alphanumeric or not.

Program Output:

Character 'H' is alphanumeric.
Character 'e' is alphanumeric.
Character 'l' is alphanumeric.
Character 'l' is alphanumeric.
Character 'o' is alphanumeric.
Character '1' is alphanumeric.
Character '2' is alphanumeric.
Character '3' is alphanumeric.
Character '!' is not alphanumeric.