C isalnum()

The isalnum() function defined in the ctype.h header file. It helps to check the specified character is alphanumeric or not. The character is said to be alphanumeric it will be an alphabet or number.


int isalnum(int argument); #where argument will be a character
 

isalnum() Parameters:

The isalnum() function takes a single parameter.

Parameter Description Required / Optional
argument  The character to be checked Required

isalnum() Return Value

The return value is either 1 or 0.

Input Return Value
argument is an alphanumeric  1
argument is neither an alphabet nor a digit 0

Examples of isalnum()

Example 1: The working of isalnum() function in C


#include <stdio.h>
#include <ctype.h>
int main()
{
    char ch;
    int output;

    ch = '10';
    output = isalnum(ch);
    printf("If %c is passed, return value is %d\n", ch, output);

    ch = 'A';
    output = isalnum(ch);
    printf("If %c is passed, return value is %d\n", ch, output);

    ch = 'B';
    output = isalnum(ch);
    printf("If %c is passed, return value is %d\n", ch, output);

    ch = '+';
    output = isalnum(ch);
    printf("If %c is passed, return value is %d\n", ch, output);

    return 0;
}

Output:


If 10 is passed, return value is 1
If A is passed, return value is 1
If B is passed, return value is 1
If + is passed, return value is 0

Example 2: How to check if a character is an alphanumeric character or not?


#include <stdio.h>
#include <ctype.h>
int main()
{
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);

    if (isalnum(ch) == 0)
        printf("The given %c is not an alphanumeric character.", ch);
    else
        printf("The given %c is an alphanumeric character.", ch);
    
    return 0;
}

Output:


Enter a character: 0
The given 0 is an alphanumeric character.