Rewrite C Function: isprint

Last Updated : 10 July, 2022 · 2 min read

articles banner image

In this tutorial, we have presented relevant information that we have gathered about the isprint function for your convenience. Among other things, here you will find:

Structure of isprint:

Declaration: int isprint(int c);

Return: non-zero value if int c is a printable value. Otherwise, 0.

Parameter list:
(int c) - the character that is checked to determine the return value.

Header file: <ctype.h>.

1. Now, what is the isprint function in C?

The isprint function is in the C Standard Library for a very specific purpose. That is, to check if a received parameter is a printable character. Whether or not a character is printable is concluded by its ASCII value. Say, a value of a character is within a range of 32 and 126, then it is, in fact, a printable char.

The isprint function checks to determine if the received value is within the mentioned range and returns an appropriate result. As you might have already seen it in the Structure of isprint table above, this function returns 0, if a received parameter is not a printable character and a non-zero value, if it is printable.

Many compilers return a fixed 1 as a non-zero value, yet different non-zero values can be observed among different compilers, too.

Example 1: usage of the isprint function in C.

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

int main( void )
{
    
printf ( "1. %d\n", isprint ( 'a' ));
    
printf ( "2. %d", isprint ( 10 ));
    
return ( 0 );
}

Possible output:

1. 1
2. 0

2. How to rewrite the isprint function in C?

In order to implement the isprint function we would need to check if a given argument is a printable character and return appropriate integer value. An instance of this is available to you in the code snippet below (Example 2).

Example 2: Rewritten isprint function in C.

int our_isprint ( int c )
{
    
if ( c >= 32 && c <= 126 )
        
return ( 1 );
    
return ( 0 );
}

Here you can see a conditional statement (on line 3), that returns true if the c value is, in fact, within the range of printable characters. In such a case, the our_isprint function returns 1. Otherwise, 0 will be returned, indicating that the received c value is not printable.

3. Testing the rewritten isprint function

Given that our implementation of the isprint function is accurate, we would expect to see a correspondence between zero and non-zero values based on different arguments. Let's test to see, if it is the case.

Example 3: Test of the rewritten isprint function in C.

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

int our_isprint ( int c )
{
    
if ( c >= 32 && c <= 126 )
        
return ( 1 );
    
return ( 0 );
}
int main( void )
{
    
printf ( "1. our_isprint: %d, isprint: %d\n" , our_isprint ( 'a' ), isprint ( 'a' ));
    
printf ( "2. our_isprint: %d, isprint: %d\n" , our_isprint ( 27 ), isprint ( 27 ));
    
return ( 0 );
}

Possible output:

1. our_isprint: 1, isprint: 1
2. our_isprint: 0, isprint: 0

The tested values above show consistency between implemented and standard isprint functions. Eventhough, some compilers may resort to different non-zero values, as to indicate that received value is printable, our implementation is still principally consistent. Because, as of June 2022, there are no specifications for the correct and only non-zero values that need to be returned from the isprint function.

4. Quiz of the reader's knowledge

Quiz : Rewrite C Function (isprint)

1What is the return value of isprint('?')?

2Which condition satisfies the range of printable characters?

2What is the return value of isprint(127)?

quiz completed!

Congrats!

Feel free to share your achievement below!