The isgraph function in C programming checks whether a character is a graphic character or not. The character that has graphical representation are known are a graphic character.
It is declared in ctype.h
and takes one argument in the form of integer and returns the value of type int. If the character passed is a graphic character, it returns a non-zero integer. If not, it returns 0. In the default C locale, the following characters are graphic:
- digits (
0123456789
) - uppercase letters (
ABCDEFGHIJKLMNOPQRSTUVWXYZ
) - lowercase letters (
abcdefghijklmnopqrstuvwxyz
) - punctuation characters (
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
)
Note:
The isgraph function tests for any printing character except space (' '
).
Syntax of isgraph function in C:
//Syntax of isgraph int isgraph(int c);
Parameters:
c
=> character to classify
Return value:
Non-zero value
=> If the argument is a graphic character.
0
=> If the argument is neither a graphic character.
Example,
Input : 'a' Output : Non-zero value Input : ';' Output : Non-zero value Input : ' ' Output : Zero
C program to understand the working of isgraph function:
Consider the below code where I am passing different characters in the isgraph function. You can see the output of this function with different characters.
#include <stdio.h> #include <ctype.h> int main() { unsigned char c = 'A';; int result = isgraph(c); result ? printf("A is graphical char\n"):printf("A is not a graphical char\n"); c = '@'; result = isgraph(c); result ? printf("@ is graphical char\n"):printf("@ is not a graphical char\n"); c = '\n'; result = isgraph(c); result ? printf("\\n is graphical char\n"): printf("\\n is not a graphical char\n"); c = ';'; result = isgraph(c); result ? printf("; is graphical char\n"): printf("; is not a graphical char\n"); return 0; }
Output:
C Program To Print all Graphical characters using the isgraph():
Let’s C a C program to print default C graphical characters.
#include <stdio.h> #include <ctype.h> int main() { unsigned int i; printf("All Graphical char in C: \n\n"); // looping through all ASCII characters for (i = 0; i <= 127; ++i) { if(isgraph(i)!= 0) { printf("%c ", i); } } printf("\n\n"); return 0; }
Output:
Note:
If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of isgraph is undefined.
As we know the behavior of isgraph is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.
int my_isgraph(char ch) { return isgraph((unsigned char)ch); }
C Program To Print all characters of a string until not get not non-graphical characters using the isgraph():
The below-mentioned C code prints a string character by character until not get a not graphical character. When not graphical character met breaks the while-loop. In this code, only the “Aticleworld” would be printed, since the line ends with a newline character (‘\n
‘), which is not a graphical character.
#include <stdio.h> #include <ctype.h> int main () { int i=0; char str[]="Aticleworld\n is good website to learn C\n"; while (isgraph((unsigned char)str[i])) { putchar ((unsigned char)str[i]); i++; } return 0; }
Output:
Aticleworld
Recommended Post:
- Use of isxdigit in C programming.
- How to use ispunct function in C programming?
- tolower function in C.
- How to use the islower function in C?
- Use of iscntrl function in C.
- How to use isalpha function in C programming?
- Use isalnum function in C programming?
- How to use isdigit function in C programming?
- How to use sizeof operator in C.
- _Alignof or alignof Operator in C
- Alignment specifiers in C ( _Alignas).
- Function Specifiers in C.
- Type qualifiers in C.