w3resource

C isalnum() function

C isalnum(int ch)

The isalnum() function is used to check whether the argument ch is a character of class alpha or digit in the current locale. The function is defined in the ctype.h header file.

Syntax:

int isalnum(int argument);

isalnum() Parameters:

Name Description Required /Optional
ch Argument ch represents a character. Required

isalnum() Return Value:

  • The isalnum() function returns non-zero if ch is an alphanumeric character; otherwiset returns 0.

Example-1: isalnum() function return value

#include <stdio.h>
#include <ctype.h>
int main()
{
    char ch;
    int result;
    ch = '8';
    result = isalnum(ch);
    printf("Return value is %d When %c is passed as an argemdent.", result, ch);
    ch = '$';
    result = isalnum(ch);
    printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);
    ch = '+';
    result = isalnum(ch);
    printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);
    ch = 'A';
    result = isalnum(ch);
    printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);  
	ch = '-';
    result = isalnum(ch);
    printf("\nReturn value is %d When %c is passed as an argemdent.", result, ch);   
    return 0;
}

Output:

Return value is 4 When 8 is passed as an argemdent.
Return value is 0 When $ is passed as an argemdent.
Return value is 0 When + is passed as an argemdent.
Return value is 1 When A is passed as an argemdent.
Return value is 0 When - is passed as an argemdent.

Example #2: Check whether a character is an alphanumeric or not!

#include <stdio.h>
#include <ctype.h>
int main()
{
    char ch;
    printf("Input a character: ");
    scanf("%c", &ch);
    printf("Check the said number is alphanumeric characte or not!");
	    if (isalnum(ch) == 0)
        printf("\n%c is not an alphanumeric character.", ch);
    else
        printf("\n%c is an alphanumeric character.", ch);
    return 0;
}

Output:

Input a character: J
Check the said number is alphanumeric characte or not!
J is an alphanumeric character.

C Programming Code Editor:

Previous C Programming: C type Home
Next C Programming: C isalpha()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Reading a string with scanf :

An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

Ref : https://bit.ly/3pdEk6f