isgraph() Function
The isgraph()
function in C checks whether a character has a graphical representation, meaning it verifies if the character is printable and not a space. This function is useful when you need to filter out non-visible characters from user input or text processing operations.
Syntax of isgraph()
int isgraph(int c);
Parameters
Parameter | Description |
---|---|
c | An integer representing the character to be checked, typically cast from a char or EOF. |
Note that the isgraph()
function considers all printable characters except for the space character (‘ ‘).
Return Value
The function returns a nonzero value (true) if the character has a graphical representation, and zero (false) if it does not.
Examples for isgraph()
Example 1: Checking a Printable Character
This example demonstrates how to use isgraph()
to verify if a given character is graphical.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = '!';
if (isgraph(ch)) {
printf("The character %c has a graphical representation.\n", ch);
} else {
printf("The character %c does not have a graphical representation.\n", ch);
}
return 0;
}
Explanation:
- A character variable
ch
is initialized with the value'!'
. - The
isgraph()
function checks whether'!'
has a graphical representation. - Since
'!'
is a printable character and not a space, the function returns true. - The program prints a message confirming that the character has a graphical representation.
Program Output:
The character ! has a graphical representation.
Example 2: Evaluating a Space Character
This example illustrates how isgraph()
returns false for the space character, which does not have a graphical representation.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = ' ';
if (isgraph(ch)) {
printf("The character (space) has a graphical representation.\n");
} else {
printf("The character (space) does not have a graphical representation.\n");
}
return 0;
}
Explanation:
- The character
' '
(space) is assigned to the variablech
. - The
isgraph()
function checks if the space character has a graphical representation. - Since space is excluded from graphical characters, the function returns false.
- A message is printed indicating that the space character does not have a graphical representation.
Program Output:
The character (space) does not have a graphical representation.
Example 3: Processing a String of Characters
This example demonstrates how to iterate over a string and use isgraph()
to determine which characters are graphical.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hi there!";
int i = 0;
while (str[i] != '\0') {
if (isgraph(str[i])) {
printf("Character '%c' is graphical.\n", str[i]);
} else {
printf("Character '%c' is not graphical.\n", str[i]);
}
i++;
}
return 0;
}
Explanation:
- A string
"Hi there!"
is defined. - The program loops through each character of the string.
- For every character,
isgraph()
checks if it is a printable graphical character (excluding spaces). - Appropriate messages are printed for each character based on whether they are graphical or not.
Program Output:
Character 'H' is graphical.
Character 'i' is graphical.
Character ' ' is not graphical.
Character 't' is graphical.
Character 'h' is graphical.
Character 'e' is graphical.
Character 'r' is graphical.
Character 'e' is graphical.
Character '!' is graphical.