Isprint Function In C

This function receives a user-provided character type argument and verifies if the character is considered printable. In case the character is indeed printable, the function outputs a non-zero positive value; otherwise, it returns zero (0).

For instance, assuming the user provides the argument 'g' to the isprint function, the function validates whether the input character is displayable or not. In case it is, the function outputs a value other than zero. Likewise, if we pass a newline (\n) or tab (\t) character to the isprint function, it will yield zero as these are non-printable characters.

Syntax of the isprint function

Below is the format of the isprint function in the C programming language:

Example

int isprint (char c);

Parameter:

It signifies that the data type representing a character is supplied to the isprint function.

Return Value:

The isprint function examines the provided parameter, and if the character is displayable, it yields a value other than zero. If not, it returns zero.

Example 1: Code to identify the displayable character utilizing the isprint method

Let's create a basic program that identifies the displayable character on the screen by utilizing the isprint function within the C programming language.

Example

/* program to check the printable character using the isprint() function in C. */
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main ()
{
	// declaration of the character type variables
	int ch1 = 'b';
	int ch2 = '@';
	int ch3 = ' ';
	int ch4 = '%'; 
	int ch5 = '\n';
	int ch6 = '\t';
	
	/* use isprint () function to check the printable character. */
	if ( isprint (ch1) )
	{
		printf (" '%c' is a printable character. \n", ch1);
	}
	else
	{
		printf ("'%c' is not a printable character. \n", ch1);
	}
	
	
	if ( isprint (ch2) )
	{
		printf (" '%c' is a printable character. \n", ch2);
	}
	else
	{
		printf ("'%c' is not a printable character. \n", ch2);
	}

	
	if ( isprint (ch3) )
	{
		printf (" '%c' is a printable character. \n", ch3);
	}
	else
	{
		printf ("'%c' is not a printable character. \n", ch3);
	}


	if ( isprint (ch4) )
	{
		printf (" '%c' is a printable character. \n", ch4);
	}
	else
	{
		printf ("'%c' is not a printable character. \n", ch4);
	}	
	
	if ( isprint (ch5) )
	{
		printf (" '%c' is a printable character. \n", ch5);
	}
	else
	{
		printf ("'%c' is not a printable character. \n", ch5);
	}	
	
	if ( isprint (ch6) )
	{
		printf (" '%c' is a printable character. \n", ch6);
	}
	else
	{
		printf ("'%c' is not a printable character. \n", ch6);
	}
	
	return 0;
}

Output:

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 provided code snippet, a range of characters such as 'b', '@', ' ', '%', newline (\n), and tab (\t) are explicitly specified to determine their printability. Following this, the isprint function examines and displays only the printable characters. In cases where a character is deemed non-printable, the function outputs "%c is not a printable character".

Example 2: Script that prompts the user to input a character and verifies if the entered character is displayable or not.

Let's develop a program that prompts the user to input a character and then demonstrates its display using the isprint function within the C programming language.

Example

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

int main ()
{
	// declaration of the variables 
	char c;
	

	printf (" \n Input a printable character: ");
	scanf (" %c", &c); // get a character
	
	/* use the isprint() function to print only printable character */
	if (isprint (c))
	{
		printf (" \n '%c' is a correct printable character. ", c);
	}
	else
	{
		printf (" \n '%c' is not a printable character. ", c);
		printf (" \n Input only printable character. " );
	}
	return 0;
}

Output:

Output

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

In the provided code snippet, we accept the character 'j' as input from the user and subsequently employ the isprint function to verify whether the character 'j' is indeed a valid printable character.

2 nd execution:

Example

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

Similarly, we request another input character or digit '8' from the user, which is then evaluated by the isprint function to verify if it is a printable character. Subsequently, the isprint function confirms that '8' is indeed a valid printable character. However, if the user enters a new line (\n) or a tab (\t), the isprint function will output that %c is not a printable character.

Develop a program that verifies the printability of a string by employing the isprint function in conjunction with a while loop.

Let's create a sample code snippet that utilizes a while loop and the isprint function in C to display printable string characters.

Example

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

int main ()
{
	// declare a character type string
	char str[] = "Zb. \n2 \t*.";
	int i = 0;
	
	// use while loop to iterate string character one by one 
	while (str[i] != '\0')
	{
	/* use the isprint() function to check the valid printable string character */
	if (isprint (str[i]))
	{
		printf (" \n '%c' is a printable character. ", str[i]);
	}
	else
	{
		printf (" \n %c is not a printable character. ", str[i]);
	}
	i++;	
	}
	return 0;
}

Output:

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 this program snippet, a character string "Ab. \n2 \t*." is initialized, followed by a validation process to determine if the string is printable. Initially, a while loop is employed to iterate through each character of the string until it reaches the end. Within this loop, each character is assessed for printability using the isprint function, with all valid characters, including white spaces, being displayed. If a character is deemed non-printable, the program outputs "It is not a printable character."

Example 4: Code to identify all displayable characters in C with the isprint function

The isprint function in C is a part of the C Standard Library's ctype.h header file. This function is utilized to determine whether a character is printable or not based on the ASCII value of the character. In the ASCII table, printable characters are those that include numbers, alphabets, special characters such as punctuation marks, and whitespace characters like spaces and tabs.

Below is a simple program that demonstrates the application of the isprint function to identify all printable characters in C:

Example

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

int main() {
    int i;

    printf("Printable characters in ASCII:\n");
    for(i = 0; i <= 127; i++) {
        if(isprint(i)) {
            printf("%c ", i);
        }
    }

    return 0;
}

In this code snippet:

  • We first include the necessary header files, i.e., stdio.h and ctype.h.
  • We then define a loop that iterates through ASCII values from 0 to 127.
  • Within the loop, the isprint function is used to check if the current ASCII value represents a printable character.
  • If the character is printable, it is displayed on the screen.

By running this program, you can observe all the printable characters in the ASCII table being identified and printed on the console.

Let's develop a script to identify the entire sets of displayable characters in the C programming language.

Example

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main ()
{
	// declaration of the variables
	int i = 0;
	int count = 0; // count printable characters
	
	// use while loop to iterate till defined condition
	while ( i < 256)
	{
		/// use isprint() function to check printable character
		if ( isprint(i) != 0)
		{
			printf (" %c", i); // it prints only printable character
			++count; // count total printable character and update the count by 1
		}	
		++i; 
	}
	printf (" \n There are total %d printable characters. \n", count);
	
	getch();
}

Output:

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: Code snippet to display all printable characters within an array by leveraging the isprint function

Let's develop a script to display all elements of an array containing character types by utilizing the isprint function within the C programming language.

Example

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h>
int main() 
{ 
           // declare a character type array
	char str[8] = {'G', '&', ' ', '6', '\n', 'e',  '!', '\t'};  

	// It includes 0 as the return values of all defined character in str array.
	int arr[8] = {0, 0, 0, 0, 0, 0, 0};  
	int i; // declare 'i' as int type variable
	
// iterate all elements of the array using the for loop
    for (i = 0; i < 8; i++)
    {
      arr[i] = isprint (str[i]); /* the isprint() function check printable character and assign character to arr[i] */
    }
   
    // iterate to display characters
    for (i = 0; i < 8; i++)
    {
      printf( "\n '%c'", str[i]); 
      
      // if statement checks all the characters of the array that is not equal to 0.
	  if (arr[i] != 0)
	  {
	  	printf( " is a printable character. ");	
	  }
      else     
	  {
	  	printf( " is NOT a printable character. ");	
	  }
    }
    return 0; 
}

Output:

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.

Input Required

This code uses input(). Please provide values below: