The isgraph function is declared in the ctype.h header file. If the passed character is a graphical character, the isgraph function returns a non-zero value. And if the given character is not the graphical or printable character, the function returns a zero value.
For example, suppose we enter characters 'u', 6, / to the isgraph function and then function check whether the input character is graphical or not. Here we pass different characters that return a non-zero value. Similarly, we passed the white space (' ') character to the isgraph function that returns zero value.
Syntax of the isgraph function
Below is the format of the isgraph function in the C programming language.
int isgraph( int c);
Here, the isgraph function includes a single parameter to verify valid graphic characters.
Parameter:
The variable 'c' denotes the character supplied to the isdigit function.
Return Value:
The isgraph function checks if the character provided is a printable character and returns a non-zero value if true, or zero if false.
Example 1: Program to check whether the passed character is the graphical characters
Let's create a basic program to validate if the provided characters are graphical characters in the C programming language.
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main ()
{
// declare character type variable
/* use the isgraph () function to check the graphical character. */
if ( isgraph ('@') )
{
printf (" It is a graphical character. \n" );
}
else
{
printf (" It is not a graphical character. \n");
}
// check for new line
if ( isgraph ('\n' ))
{
printf (" It is a graphical character. \n" );
}
else
{
printf (" It is not a graphical character. \n");
}
// check for 'ch character
if ( isgraph ('Ch') )
{
printf (" It is a graphical character. \n" );
}
else
{
printf (" It is not a graphical character. \n");
}
// check for new '\t' tab
if ( isgraph ('\t' ))
{
printf (" It is a graphical character. \n" );
}
else
{
printf (" It is not a graphical character. \n");
}
// check for '#' symbol
if ( isgraph ('#') )
{
printf (" It is a graphical character. \n" );
}
else
{
printf (" It is not a graphical character. \n");
}
// check for white space
if ( isgraph ( ' ') )
{
printf (" It is a graphical character. \n" );
}
else
{
printf (" It is not a graphical character. \n");
}
return 0;
}
Output:
It is a graphical character.
It is not a graphical character.
It is a graphical character.
It is not a graphical character.
It is a graphical character.
It is not a graphical character.
Example 2: Program to take an input from the user to validate the graphical character
Let's create a C program that allows users to input and verify graphical characters using the isgraph function.
/* input a number from the user to display graphical characters. */
#include <stdio.h>
#include <ctype.h>
int main ()
{
// declaration of char variables
char c;
printf (" \n Enter the graphical character: ");
scanf (" %c", &c); // get a graphical character
/* use the isgraph() function to check graphical characters */
if (isgraph (c))
{
printf (" \n '%c' is a graphical character. ", c);
}
else
{
printf (" \n '%c' is not the graphical character. ", c);
printf (" \n Please, enter only graphical character. " );
}
return 0;
}
Output:
Enter the graphical character: 5
'5' is a graphical character.
In the provided code snippet, the number 5 is provided by the user to determine if it qualifies as a graphical character. If 5 is recognized as a graphical character, the output will indicate '5 is a graphical character'. This validation is performed using the isgraph function with 5 passed as its parameter for verification.
2 nd execution:
Enter the graphical character: *
'*' is a graphical character.
Here, the symbol '' is provided as input to the isgraph function to display a printable character. When the input character is indeed a printable character, the function outputs " is a printable character".
Similarly, if we provide a white space ('') or newline (\n) character as input to the isgraph function to check for graphical character validation, the function will determine that the white space or newline character is not a graphical character, resulting in the return message "It is not a graphical character".
Example 3: Program to print all graphical characters in C
Let's create a basic script to verify and showcase all the available graphical symbols within the C programming dialect.
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main ()
{
// declare and initialize the variables
int i = 0;
int count = 0; // it counts the total graphical characters
// This loop continuously iterates till condition is satisfied
while ( i < 256)
{
/// use isgraph() function to validate the graphical character
if ( isgraph(i) != 0)
{
printf (" %c", i); // It only print the graphical characters
++count; // update the count by 1
}
++i; // increment i by 1
}
printf (" \n There are total %d valid graphical characters. \n", count);
getch();
}
Output:
! " # $ % & ' ( ) * + , - . / 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 { | } ~
There are total 94 valid graphical characters.
Utilizing the isgraph function within the given code snippet enables the display of all graphical characters present in the C library. To facilitate this, we initialized 'i' and 'count' as integer variables set to 0, aiding in the printing and tallying of valid graphical characters within the C language. The iterative process is maintained by a while loop that persists until the condition (i < 257) is met, ensuring the continual printing of graphical characters. Upon completion, the program outputs the total count of valid graphical characters identified.