Rewrite C Function: isgraph

Last Updated : 10 July, 2022 · 2 min read

In this article, you will:

Structure of isgraph:

Declaration: int isgraph(int c);

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

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

Header file: <ctype.h>.

1. So, what is the isgraph function in C and how to use it?

The isgraph function in C programming language tests if a character has a graphic representation. A graphic representation is, in turn, synonymous to visual display. So, if a character can be displayed on the screen, then it is considered to be graphically representable and the return value of isgraph indicates just that.

If you payed a closer attention to the structure of isgraph above, you will already know that this library function returns a non-zero value, given that a character has a visual display. In other case, 0 will be returned from this C function.

If you are familiar with other functions in the ctype.h library - you may be wondering, how is, in fact, the isgraph function any different from the isprint. And they only differ in a single case. When 32 (a space character) is passed as an argument to these functions, then isgraph function, returns 0, indicating that a space is not a graphic character. The isprint function, on the other hand, returns a non-zero value with the value of 32 as an input parameter.

Note: all the ASCII values between 33 and 126 ([33, 126]) can be visually displayed.

Example 1: usage of the isgraph function in C

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

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

Possible output:

1. 1
2. 0

2. How to rewrite the isgraph function in C?

Consider the code example below for a moment:

Example 2: rewritten isgraph function in C

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

The our_isgraph function above takes in one parameter int. If we pass a char argument to this function, then it gets implicitly converted to an approriate ASCII value.

Now, if this value happens to be within the range of 33 and 126, then our_isgraph function returns a non-zero value. To be exact, it returns 1. 0 is returned if an only if the c is not within the mentioned range.

3. Testing the rewritten isgraph function

We can test our_isgraph and isgraph functions by proving them with identical input arguments and checking for their return values. Just as it is done in the code snippet below.

Example 3: test of the rewritten isgraph function in C

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

int our_isgraph ( int c )
{
    
if ( c >= 33 && c <= 126 )
        
return ( 1 );
    
return ( 0 );
}
int main( void )
{
    
printf ( "1. our_isgraph: %d, isgraph: %d\n" , our_isgraph ( ' ' ), isgraph ( ' ' ));
    
printf ( "2. our_isgraph: %d, isgraph: %d\n" , our_isgraph ( 120 ), isgraph ( 120 ));
    
return ( 0 );
}

Possible output:

1. our_isgraph: 0, isgraph: 0
2. our_isgraph: 1, isgraph: 1

As you can see, our implementation of isgraph function returns, in this case, exactly the same results as does the C library function.

Note: different compilers may return a different non-zero value than 1, in case a passed character has a graphic representation.

4. Quiz of the reader's knowledge

Quiz : Rewrite C Function (isgraph)

1What is the return value of isgraph('u')?

2In which library is isgraph defined?

2What is the return value of isgraph(100)?

quiz completed!

Congrats!

Feel free to share your achievement below!