C isalnum()

isalnum() 函数定义在 ctype.h 头文件中。它有助于检查指定字符是否为字母数字。如果字符是字母或数字,则称其为字母数字。


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

isalnum() 参数

isalnum() 函数接受一个参数。

参数 描述 必需/可选
参数 要检查的字符 必需

isalnum() 返回值

返回值为 1 或 0。

输入 返回值
参数是字母数字 1
参数既不是字母也不是数字 0

isalnum() 示例

示例 1:C 语言中 isalnum() 函数的工作原理


#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;
}

输出


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

示例 2:如何检查字符是否为字母数字字符?


#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;
}

输出


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