isgraph() function in C

This topic will discuss the isgraph() function in the C programming language. The isgraph() function in C programming is used to check whether a character is a graphic character or not.

The graphic characters are those graphical representation characters used to display or print characters on the screen. It means the graphical character prints passed characters without displaying whitespace, newline, and new tab characters like (' '), '\n', '\t', which is not considered as the isgraph() characters.

isgraph() function in C

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

The following is the syntax of the isgraph() function in C.

Here, the isgraph() function contains one argument to check valid graphical characters.

Parameter:

C - The 'c' variable represents the character passed in the isdigit() function.

Return Value:

The isgraph() function determines whether the passed character is a graphic character and if yes, the function returns a non-zero value or returns zero value.

Example 1: Program to check whether the passed character is the graphical characters

Let's write a simple program to check whether the given characters are the graphical characters in the C language.

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 write the C program to input and validate the graphical character using the isgraph() function.

Output:

Enter the graphical character: 5
'5' is a graphical character.

In the above program, we input 5 from the user to check whether the passed character is a graphical character. If the 5 is a graphical character, it displays '5 is a graphical character'. So, we use the isgraph() function takes 5 as an argument to validate the graphical character.

2nd execution:

Enter the graphical character: *
'*' is a graphical character.

Here, the character '*' is passed as an argument to the isgraph() function for printing the graphical character. And if the given character is a graphical character, the function shows "* is a graphical character".

Similarly, when we pass the white space ('') or newline (\n) character to the isgraph() function for validating the graphical character. Here, the '' white space or new line is not a graphical function, so the isgraph() returns "It is not a graphical character".

Example 3: Program to print all graphical characters in C

Let's write a simple program to check and display all the existing graphical characters in the C programming language.

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.

In the above program, we use the isgraph() function to print all the graphical characters available in the C library. So, we declared and initialized the integer variables 'i' and 'count' with 0 to print and count the valid graphical characters in C. The while loop continuously iterates and prints the graphical character till (i < 257) condition is satisfied. And finally, it prints the total valid graphical characters.