C Library Function isprint( )


In this tutorial, you will learn about C library function isprint() which is a character handling function used to check whether its argument can be displayed on the screen (including the space character) or not.

C Library Function isprint( )

As discussed earlier, we have to include ctype.h header file for using isprint( ) standard character library function.

#include <ctype.h>

C Library Function isprint( ): Function prototype


int isprint( int ch);

This function checks whether its argument can be displayed on the screen (including the space character) or not.

It returns a true value if its argument is a printing character including a space and zero (0) otherwise.

Demonstration of C isprint( )


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

int main()
{
   printf("Demonstration of isprint():\n");
   printf("\n%s%s", isprint('$') ? "$ is a " : "$ is not a ", "printing character");
   printf("\n%s%s%s", "Space", isprint(' ') ? " is a " : " is not a ", "printing character");
   printf("\n%s%s%s", "Newline", isprint('\n') ? " is a " : " is not a ", "printing character");

   return 0;
}

Output

Demonstration of isprint():

$ is a printing character
Space is a printing character
Newline is not a printing character

Explanation
In the above example, standard character library function isprint determines whether its argument is a printing character or not.

Conditional operator ?: does the selection between two strings "is a" and "is not a".

Note: isprint function recognize space as a printing character.

This can be simply done with the help of if...else statements.

Example of C library function isprint() using if … else statement.


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


int main()
{
   printf("Demonstration of isprint( ):\n");

   if( isprint('$') == 0)
     printf("\n$ is not printing character");
   else
     printf("\n$ is a printing character");

   if( isprint(' ') == 0)
     printf("\nspace is not printing character");
   else
     printf("\nspace is a printing character");

   if( isprint('\n') == 0)
     printf("\nNewline is not printing character");
   else
     printf("\nNewline is a printing character");

   return 0;
}

Output

Demonstration of isprint():

$ is a printing character
Space is a printing character
Newline is not a printing character

Explanation
In this example, we have used if...else statements instead of a conditional operator.
Here, if...else statement does the selection between two strings.