isprint() function in C

This section will discuss the isprint() function in the C programming language. The isprint() function is a predefined library function of the C language, which is used to check whether the input character is a printable character on the screen (including the space character) or not. It is defined in the ctype.h header file. So, we must add the ctype.h header file while writing a program in the C language.

isprint() function in C

This function takes a character type argument from the user and validates whether the character is printable. If the passed character is printable, the function returns a positive non-zero value; else, it shows zero (0).

For example, suppose the user enters the 'g' character as an argument to the isprint() function; the function checks the input character is printable or not. And if yes, it returns a non-zero value. Similarly, if we input a new line (\n) or tab (\t) character to the isprint function, it returns zero because it is not a printable character.

Syntax of the isprint() function

Following is the syntax of the isprint() function in the C language, as follows:

Parameter:

c: It indicates the character type value is passed to the isprint() function.

Return Value:

The isprint() function checks the passed argument, and if the character is printable, it returns a non-zero value. Otherwise, it returns zeroes.

Example 1: Program to find the printable character using the isprint() function

Let's write a simple program to find the printable character on the screen using the isprint() function in the C language.

Output:

'b' is a printable character.
'@' is a printable character.
' ' is a printable character.
'%' is a printable character.
' ' is not a printable character.
'	' is not a printable character.

In the above program, we have defined various characters ('b', '@', ' ', '%', the new line (\n), and the '\t' (tab space)) to check the printable character. And then, the isprint() function check and print the only printable character, and if any character is not printable, it returns "%c is not printable character".

Example 2: Program to take a character from user and check whether the character is printable or not

Let's write a program to get a character from the user and display it using the isprint() function in the C language.

Output:

Input a printable character: j
'j' is a correct printable character.

In the above program, we input character 'j' from the user and then use the isprint() function to validate the printable character that returns 'j' is a correct printable character.

2nd execution:

Input a printable character: 8
'8' is a correct printable character.

Similarly, we take another character or digit '8' from the user, and then the isprint() function checks the given printable character. After that, the isprint() function print '8' is a correct printable character. And if enter new line (\n) or \t, the isprint() function returns %c is not a printable character.

Example 3: Program to check the printable string using the isprint() function and while loop

Let's write an example to print the printable string characters using the while loop and isprint() function in C.

Output:

'Z' is a printable character.
'b' is a printable character.
'.' is a printable character.

is not a printable character.
'2' is a printable character.
' ' is a printable character. 
	is not a printable character.
'*' is a printable character.
'.' is a printable character.

In the above program, we declare a character string "Ab. \n2 \t*." and then check whether the given string is printable or not using the isprint() function. So, first, we use a while loop that iterates each character of the string till the string is not equal to null. Inside the while loop, condition check and print each valid character using the isprint() function, including the white space. And if the character is not printable, it prints " It is not a printable character.".

Example 4: Program to find all printable characters in C using the isprint() function

Let's create a program to find the complete lists of printable 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 95 printable characters.

Example 5: Program to print all printable characters of the array using the isprint() function

Let's create a program to print all character type array elements using the isprint() function in the C programming language.

Output:

'G' is a printable character.
'&' is a printable character.
' ' is a printable character.
'6' is a printable character.
'
' is NOT a printable character.
'e' is a printable character.
'!' is a printable character.
'	' is NOT a printable character.