Check whether a character is a Chinese character in C language.
In order to determine if a character is a Chinese character, you can use the character encoding in the C language. In Unicode encoding, the range for Chinese characters is from 0x4E00 to 0x9FA5.
Here is an example code:
#include<stdio.h>
int isChineseChar(char c) {
unsigned char ch = (unsigned char)c; // 将字符强制转换为无符号字符
if(ch >= 0x4E00 && ch <= 0x9FA5) { // 判断是否在汉字编码范围内
return 1; // 是汉字
}
return 0; // 不是汉字
}
int main() {
char c;
printf("请输入一个字符:");
scanf("%c", &c);
if(isChineseChar(c)) {
printf("该字符是汉字。\n");
} else {
printf("该字符不是汉字。\n");
}
return 0;
}
The function isChineseChar() in the above code is used to determine if a character is a Chinese character. It first converts the character to an unsigned character, and then checks if its Unicode encoding is within the range for Chinese characters. If it is a Chinese character, it returns 1; otherwise, it returns 0.
In the main() function, the user’s input character is first obtained using the scanf() function. Then the isChineseChar() function is called to determine if the character is Chinese, and the corresponding prompt message is output based on the result.