C Standard Library

C <ctype.h> - isgraph() Function



The C <ctype.h> isgraph() function is used to check if the given character has a graphical representation. A character with graphical representation are those characters than can be printed (as determined by isprint) except the space character (' '). In the default "C" locale, the following characters are graphic:

  • Digits (0123456789)
  • Uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • Lowercase letters (abcdefghijklmnopqrstuvwxyz)
  • Punctuation characters (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)

Other locales may consider a different selection of characters as graphic characters.

Syntax

int isgraph ( int ch );               

Parameters

ch Specify the character to be checked, casted to an int, or EOF.

Return Value

Returns non-zero value (i.e, true) if ch has a graphical representation, else returns zero (i.e, false).

Example:

The example below shows the usage of isgraph() function.

#include <stdio.h>
#include <ctype.h>
 
int main (){
  char str[50] = "Hello \nWorld!";

  //replacing all characters which has no 
  //graphical representation with @ in str
  int i = 0;
  while(str[i]) {
    if(!isgraph(str[i]))
      str[i] = '@';
    i++;
  }

  //displaying the output
  printf("str contains: %s", str);  
  return 0;
}

The output of the above code will be:

str contains: Hello@@World!

❮ C <ctype.h> Library