
Two very similar ctype functions are isgraph() and isprint(). Only one character separates the two functions. And the isgraph() function has nothing to do with graphics. These are the topics for this week’s Lesson as I continue my exploration of the ctype functions.
As a budding C programmer, I thought the isgraph() function returns TRUE for characters that are “graphical” in nature, such as the | and - characters.
WRONG.
The isgraph() function returns TRUE for any printable character — except for a space. This range excludes the DEL character, the blank at ASCII code 127.
The isprint() function returns TRUE for all printable ASCII characters, including the space but not the DEL. I suppose the space “prints” in the way that is occupies the same portion of the space-time continuum as other characters, just minus the ink. DEL is often interpreted as a destructive backspace (backup and erase), though this behavior is inconsistent. Both functions return FALSE for DEL.
Here’s some sample code:
2026_05_30-Lesson-a.c
#include <stdio.h>
#include <ctype.h>
int main()
{
char string[] = "Nothing beats knowing the ASCII codes!\n";
char *s;
/* output isgraph() test */
puts("isgraph()");
s = string;
while( *s != '\0' )
{
putchar(*s);
s++;
}
/* testing output */
s = string;
while( *s != '\0' )
{
if( isgraph(*s) )
putchar('^');
else
putchar(' ');
s++;
}
putchar('\n');
/* output isprint() test */
puts("isprint()");
s = string;
while( *s != '\0' )
{
putchar(*s);
s++;
}
/* testing output */
s = string;
while( *s != '\0' )
{
if( isprint(*s) )
putchar('^');
else
putchar(' ');
s++;
}
putchar('\n');
return 0;
}
The code churns through string[], examining each character by applying the isgraph() and isprint() ctype functions.
For each function, the string is processed twice. First, a while loop outputs the string front-to-back. Second, each character is run through the ctype function; a caret is output to indicate which characters pass the function’s test, as shown in this sample run:
isgraph() Nothing beats knowing the ASCII codes! ^^^^^^^ ^^^^^ ^^^^^^^ ^^^ ^^^^^ ^^^^^^ isprint() Nothing beats knowing the ASCII codes! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For the isgraph() function, spaces fail the test; this is the only printable character not accepted by the function.
For the isprint() function, all printable ASCII characters pass the test, including the space.
Emulating these functions requires a test to determine whether the ASCII code is in the range of space (code 32 or 0x20) through tilde ('~' or 0x7E). Remember that the DEL character returns FALSE for both functions, so I don’t include it in this range.
For the isprint() function, the result is TRUE for all characters in the given range. For the isgraph() function, the results are TRUE, save for the space character. For my emulation, I use the isprint() function within my isgraph() function to help make this distinction:
2026_05_30-Lesson-b.c
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int isprint(int c)
{
if( c>=' ' && c<='~' )
return TRUE;
else
return FALSE;
}
int isgraph(int c)
{
if( isprint(c) && c!=' ' )
return TRUE;
else
return FALSE;
}
int main()
{
char string[] = "Nothing beats knowing the ASCII codes!\n";
char *s;
/* output isgraph() test */
puts("isgraph()");
s = string;
while( *s != '\0' )
{
putchar(*s);
s++;
}
/* testing output */
s = string;
while( *s != '\0' )
{
if( isgraph(*s) )
putchar('^');
else
putchar(' ');
s++;
}
putchar('\n');
/* output isprint() test */
puts("isprint()");
s = string;
while( *s != '\0' )
{
putchar(*s);
s++;
}
/* testing output */
s = string;
while( *s != '\0' )
{
if( isprint(*s) )
putchar('^');
else
putchar(' ');
s++;
}
putchar('\n');
return 0;
}
The isprint() function returns TRUE when ASCII character c is in the range of ' ' (space) through '~'. The DEL (code 127 or 0x7F) is excluded.
The isgraph() function uses the isprint() function in its if comparison, but ensures that character c is not the space. If so, the function returns TRUE. The space character forces the function to return FALSE.
The program’s output is the same as for the first example.
These two functions are weird. I assume that some reason exists why isprint() classifies the space as printable but isgraph() refuses to recognize it as “graphical.” This difference is probably due to some historical or otherwise relevant reason.
In next week’s Lesson I cover the isblank() and isspace() ctype functions.