C isalnum()

The function definition of isalnum() is:

int isalnum(int argument);

It is defined in the ctype.h header file.


isalnum() Parameters

  • argument - a character

isalnum() Return Value

  • Returns 1 if argument is an alphanumeric character.
  • Returns 0 if argument is neither an alphabet nor a digit.

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.
Did you find this article helpful?