C isalnum()

The isalphanum() function returns a non-zero integer if an argument (character) passed to the function is an alphanumeric (alphabet and number) character.

Function Prototype of isalnum( )

int isalnum(int argument);

When a character is passed as an argument, corresponding ASCII value (integer) of the character is passed instead of that character itself.

This function is defined in ctype.h header file.


Example #1: isalnum( ) function return value

#include <stdio.h>
#include <ctype.h>
int main()
{
    char c;
    int result;

    c = '5';
    result = isalnum(c);
    printf("When %c is passed, return value is %d\n", c, result);

    c = 'Q';
    result = isalnum(c);
    printf("When %c is passed, return value is %d\n", c, result);

    c = 'l';
    result = isalnum(c);
    printf("When %c is passed, return value is %d\n", c, result);

    c = '+';
    result = isalnum(c);
    printf("When %c is passed, return value is %d\n", c, result);

    return 0;
}

Output

When 5 is passed, return value is 1
When Q is passed, return value is 1
When l is passed, return value is 1
When + is passed, return value is 0

Example #2: Check if a character is an alphanumeric character

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

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

Output

Enter a character: 0
0 is an alphanumeric character.