C isgraph()

isgraph() 函数定义在 ctype.h 头文件中。此函数用于检查指定的字符是否为图形字符。如果字符具有图形表示,则称其为图形字符,这意味着除了空白字符之外的所有可打印字符。


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

isgraph() 参数

isgraph() 函数接受一个整数形式的参数,并且返回值也应为整数。当传递一个字符时,它会转换为与其 ASCII 值对应的整数值。

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

isgraph() 返回值

如果给定字符是图形字符,isgraph() 返回一个非零整数,否则返回零。当传递图形字符时,我们将得到一个不同的非零整数。

输入 返回值
如果参数不是图形字符
非零数字 如果参数是图形字符

isgraph() 示例

示例 1:如何在 C 中检查图形字符?


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

int main()
{
    char ch;
    int output;

    ch = ' ';
    output = isgraph(ch);
    printf("If %c is passed to isgraph() = %d\n", ch, output);

    ch = '\n';
    output = isgraph(ch);
    printf("If %c is passed to isgraph() = %d\n", ch, output);

    ch = '4';
    output = isgraph(ch);
    printf("If %c is passed to isgraph() = %d\n", ch, output); 
}
 

输出


If   is passed to isgraph() = 0
If 
If 4 is passed to isgraph() = 1

示例 2:使用 isgraph() 打印所有图形字符


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

int main()
{
    int k;
    printf("All graphic characters are: \n");
    for (k=0;k<=127;++k)
    {
        if (isgraph(k)!=0)
            printf("%c ",k);
    }
    return 0;
}
 

输出


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