Easy Tutorial
❮ C Function Gets C Exercise Example58 ❯

C Library Function - isgraph()


C Standard Library - <ctype.h>


Description

The C library function int isgraph(int c) checks if the passed character has a graphical representation.

A character with a graphical representation is any printable character except the space character (like ' ').

Declaration

Here is the declaration for the isgraph() function.

int isgraph(int c);

Parameters

Return Value

This function returns a non-zero value if c has a graphical representation, otherwise it returns 0.

Example

The following example demonstrates the use of the isgraph() function.

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

int main()
{
   int var1 = '3';
   int var2 = 'm';
   int var3 = ' ';

   if( isgraph(var1) )
   {
       printf("var1 = |%c| is printable\n", var1 );
   }
   else
   {
      printf("var1 = |%c| is not printable\n", var1 );
   }
   if( isgraph(var2) )
   {
       printf("var2 = |%c| is printable\n", var2 );
   }
   else
   {
      printf("var2 = |%c| is not printable\n", var2 );
   }
   if( isgraph(var3) )
   {
       printf("var3 = |%c| is printable\n", var3 );
   }
   else
   {
      printf("var3 = |%c| is not printable\n", var3 );
   }

   return(0);
}

Let's compile and run the above program, which will produce the following result:

var1 = |3| is printable
var2 = |m| is printable
var3 = | | is not printable

The following example prints all graphical characters:

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

Let's compile and run the above program, which will produce the following result:

All graphical characters in C: 
! " # $ % & ' ( ) * + , - . / 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 { | } ~

C Standard Library - <ctype.h>

❮ C Function Gets C Exercise Example58 ❯