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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章