当前位置: C语言 -- 标准库 -- <ctype.h> -- isprint

isprint函数


概要:
#include <ctype.h>
int isprint(int c);

描述:

该函数用于检查字符是否为打印字符(包括空格符' ')。

打印字符是在显示设备上占一个打印位置的字符;所有字母和数字都是打印字符。

该函数行为会受当前语言环境影响。对于ASCII字符集,打印字符为ASCII值大于0x1f的字符(0x7f(DEL)除外)。


参数:
int c

参数c为一个int类型整数,其值可用unsigned char类型表示或者等于宏EOF。如果参数c是其它值,函数行为是未定义的。


返回值:

如果参数c是打印字符(包括空格符' '),函数返回非0值(即true);反之,如果参数c不是打印字符(包括空格符' '),函数返回0(即false)。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
/*函数isprint范例*/

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    int i = 0;
    int count = 0;    //计数器,统计打印字符(包括空格符' ')的数量。
    const char str[] = "I am a slow walker, but I never walk backwards.";

    while(str[i])
    {
        if(isprint(str[i]))
            ++count;
        ++i;
    }

    printf("There are %d printing characters including space in the sentence.", count);

    return 0;
}

输出:

There are 47 printing characters including space in the sentence.


相关内容:
isgraph 检查字符是否为打印字符(空格符' '除外)的函数。
iscntrl 检查字符是否为控制字符的函数。
ispunct 检查字符是否为标点符号的函数。