给定一个字符,我们需要确定它是否可打印。我们还需要判断它是否是控制字符。如果一个字符占据了打印空间,它就被称为可打印字符。
对于标准 ASCII 字符集(由“C”语言环境使用),控制字符是 ASCII 代码 0x00 (NUL) 和 0x1f (US) 以及 0x7f (DEL) 之间的字符。
例子:
Input : a
Output :a is printable character
a is not control character
Input :r
Output : is not printable character
is control character
为了找出可打印字符和控制字符之间的区别,我们可以使用一些预定义的函数,这些函数在“ctype.h”头文件中声明。
i sprint()函数检查字符是否为可打印字符。isprint() 函数采用整数形式的单个参数并返回 int 类型的值。我们可以在内部传递一个 char 类型参数,它们通过指定 ASCII 值充当一个 int。
所述iscntrl判断()函数是用来检查一个字符是否是一个控制字符或没有。iscntrl() 函数也接受一个参数并返回一个整数。
输出:
// C program to illustrate isprint() and iscntrl() functions.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch = 'a';
if (isprint(ch)) {
printf("%c is printable charactern", ch);
} else {
printf("%c is not printable charactern", ch);
}
if (iscntrl(ch)) {
printf("%c is control charactern", ch);
} else {
printf("%c is not control character", ch);
}
return (0);
}
a is printable character a is not control character