C isprint()

isprint() 函数定义在 ctype.h 头文件中。它有助于检查指定字符是否为可打印字符。可打印字符是指占用打印空间的字符,我们可以说它与控制字符恰好相反。


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

isprint() 参数

isprint() 函数接受一个参数,该参数为整数形式,返回值也应为整数。当传入一个字符时,它会被转换为对应的 ASCII 整数值。

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

isprint() 返回值

如果给定字符是可打印字符,isprint() 返回一个非零整数,否则返回零。当传入一个可打印字符时,我们将得到一个不同的非零整数。

输入 返回值
如果参数不可打印
非零数字 如果参数可打印

Python 中 isprint() 方法的示例

示例 1:isprint() 函数在 C 中的工作原理


#include <stdio.h>
#include <ctype.h>
int main()
{
    char ch;

    ch = 'A';
    printf("If printable character %c is passed to isprint(): %d", ch, isprint(ch));

    c = '\n';
    printf("\nIf control character %c is passed to isprint(): %d", ch, isprint(ch));

    return 0;
}

输出


If printable character A is passed to isprint(): 1
If a control character 
 is passed to isprint(): 0

示例 2:使用 isprint() 打印所有可打印字符?


#include <stdio.h>
#include <ctype.h>
int main()
{
   int k;
   printf("All printable characters are:");
   for(k = 1; k <= 127; ++k)
    if (isprint(k)!= 0)
             printf("%c ", k);
   return 0;
}

输出


All 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 { | } ~