C isgraph()

Characters that have graphical representation are known are graphic characters.

The isgraph() checks whether a character is a graphic character or not. If the argument passed to isgraph() is a graphic character, it returns a non-zero integer. If not, it returns 0.

This function is defined in ctype.h header file


Function prototype of isgraph()

int isgraph(int argument);

The isgraph() function takes a single argument and returns an integer.

When character is passed as an argument, corresponding ASCII value of the character is passed instead of that character itself.


Example #1: Check graphic character

#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);  

Output

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

Example #2: Print all graphic characters

#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;
}

Output

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 { | } ~
Did you find this article helpful?