占据打印空间的字符被称为可打印字符。
可打印字符与控制字符正好相反,可以使用 iscntrl() 进行检查。
C isprint() 原型
int isprint( int arg );
函数 isprint()
接受一个整数形式的参数,并返回一个 int
类型的值。
尽管 isprint()
接受整数作为参数,但传递给函数的是字符。在内部,字符被转换为其 ASCII 值进行检查。
如果传递给 isprint()
的字符是可打印字符,它将返回非零整数,否则返回 0。
它定义在 <ctype.h> 头文件中。
示例:C isprint() 函数
#include <ctype.h>
#include <stdio.h>
int main()
{
char c;
c = 'Q';
printf("Result when a printable character %c is passed to isprint(): %d", c, isprint(c));
c = '\n';
printf("\nResult when a control character %c is passed to isprint(): %d", c, isprint(c));
return 0;
}
输出
Result when a printable character Q is passed to isprint(): 16384 Result when a control character is passed to isprint(): 0
示例:使用 isprint() 函数列出所有可打印字符的 C 程序。
#include <ctype.h>
#include <stdio.h>
int main()
{
int c;
for(c = 1; c <= 127; ++c)
if (isprint(c)!= 0)
printf("%c ", c);
return 0;
}
输出
The printable characters are: ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~