Cover Image for isgraph() function in C
83 views

isgraph() function in C

The isgraph() function is used to check whether a character is a graphic character or not. A graphic character is any character that is printable and visible when displayed, such as letters, digits, punctuation marks, and other printable symbols. In contrast, control characters (like newline, tab, and backspace) are not considered graphic characters.

The isgraph() function is part of the C Standard Library and is defined in the ctype.h header.

Here’s the prototype of the isgraph() function:

C
#include <ctype.h>

int isgraph(int c);
  • c: An integer that represents a character (or the result of getc() or fgetc()).

The isgraph() function returns a non-zero value (true) if the character c is a graphic character, and it returns 0 (false) if the character is not a graphic character.

Here’s an example of how to use the isgraph() function:

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

int main() {
    char ch;

    printf("Enter a character: ");
    ch = getchar();

    if (isgraph(ch)) {
        printf("'%c' is a graphic character.\n", ch);
    } else {
        printf("'%c' is not a graphic character.\n", ch);
    }

    return 0;
}

In this example:

  • We use getchar() to read a character from the user.
  • We then use isgraph() to check if the entered character is a graphic character or not.
  • Depending on the result, we print a message indicating whether the character is a graphic character or not.

The isgraph() function is often used when dealing with character validation or filtering in C programs, especially when you want to ensure that the input contains only visible and printable characters.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS