C isgraph()

具有图形表示的字符称为图形字符。

isgraph() 检查字符是否为图形字符。如果传递给 isgraph() 的参数是图形字符,则返回一个非零整数。如果不是,则返回 0。

此函数定义在 <ctype.h> 头文件中


isgraph() 的函数原型

int isgraph(int argument);

isgraph() 函数接受一个参数并返回一个整数。

当字符作为参数传递时,传递的是该字符对应的 ASCII 值,而不是字符本身。


示例 #1:检查图形字符

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

    c = ' ';
    result = isgraph(c);
    printf("When %c is passed to isgraph() = %d\n", c, result);

    c = '\n';
    result = isgraph(c);
    printf("When %c is passed to isgraph() = %d\n", c, result);

    c = '9';
    result = isgraph(c);
    printf("When %c is passed to isgraph() = %d\n", c, result);  

输出

When   is passed to isgraph() = 0
When 
 is passed to isgraph() = 0
When 9 is passed to isgraph() = 1

示例 #2:打印所有图形字符

#include <stdio.h>
#include <ctype.h>
int main()
{
    int i;
    printf("All graphic characters in C programming are: \n");
    for (i=0;i<=127;++i)
    {
        if (isgraph(i)!=0)
            printf("%c ",i);
    }
    return 0;
}

输出

All graphic characters in C programming 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 { | } ~
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战