C/C++ isprint函数

检查给定的字符能否被打印,即为数字( 0123456789 )、大写字母( ABCDEFGHIJKLMNOPQRSTUVWXYZ )、小写字母( abcdefghijklmnopqrstuvwxyz )、标点字符( !"#$%&’()*+,-./:;<=>?@[]^_`{|}~ )或空格之一,或任何当前 C 本地环境分类为可打印的字符。

若 ch 的值不能表示为 unsigned char 且不等于 EOF ,则行为未定义。

For the standard ASCII character set (used by the “C” locale), printing characters are all with an ASCII code greater than 0x1f (US), except 0x7f (DEL).

ASCII

在这里插入图片描述

C++
#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(): 1
Result when a control character 
 is passed to isprint(): 0
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;
}

输出结果为:

  ! " # $ % & ' ( ) * + , - . / 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 { | } ~ 

前面有个空格,空格可以打印哦。

参考

http://www.cplusplus.com/reference/cctype/isprint/

https://zh.cppreference.com/w/c/string/byte/isprint

发布了231 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/LU_ZHAO/article/details/105040324