<ctype.h>
头文件中,用来判断一个字符是否为 ASCII 码字符(ASCII 码值在 0~127 之间)。int isascii(int c);
#include <stdio.h> #include <ctype.h> int main() { char str[] = { 'A', 'b', '9', 128, -1, '\0' }; // 包括非 ASCII 字符 for(int i = 0; str[i] != '\0'; i++) { if(isascii(str[i])) { printf("'%c' is a valid ASCII character.\n", str[i]); } else { printf("Character at index %d is NOT a valid ASCII character.\n", i); } } return 0; }输出结果为:
'A' is a valid ASCII character.
'b' is a valid ASCII character.
'9' is a valid ASCII character.
Character at index 3 is NOT a valid ASCII character.
Character at index 4 is NOT a valid ASCII character.
int isascii(int c) { return (c >= 0) && (c <= 127); }这个简单的函数会检查输入字符是否在 ASCII 范围内,并返回相应的真或假值。
本文链接:http://task.lmcjl.com/news/6190.html