C Library- isgraph() function



The C ctype library isgraph() function checks if the character has graphical representation. The characters with graphical representations are all those characters that can be printed except for whitespace characters (like ' '), which is not considered as isgraph characters. This includes all characters that produce visible marks on a display, such as letters, digits, punctuation marks, etc., but excludes whitespace characters like space, tab, or newline.

Syntax

Following is the C library syntax of the isgraph() function −

int isgraph(int c);

Parameters

This function accepts a single parameter −

  • int c : This is the character to be checked, passed as an integer. The character is typically passed in its ASCII value.

Return Value

The function returns a non-zero value (true) if the character is a printable character except for the space character. It returns 0 (false) if the character is not a printable character excluding space.

Example 1: Checking Alphabetic Characters

The character 'A' is an alphabetic character and is graphically printable, thus isgraph() returns true.

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

int main() {
   char ch = 'A';

   if (isgraph(ch)) {
      printf("'%c' is a printable character other than space.\n", ch);
   } else {
      printf("'%c' is not a printable character or it is a space.\n", ch);
   }
   return 0;
}

Output

The above code produces following result −

'A' is a printable character other than space.

Example 2: Checking a Space Character

Spaces are not considered graphic characters. So, the isgraph() function returns false.

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

int main() {
   char ch = ' ';

   if (isgraph(ch)) {
      printf("'%c' is a printable character other than space.\n", ch);
   } else {
      printf("'%c' is not a printable character or it is a space.\n", ch);
   }
   return 0;
}

Output

After execution of above code, we get the following result −

' ' is not a printable character or it is a space.
Advertisements