C programming isgraph ctype.h header


In C programming the <ctype.h> isgraph function test if the character is graphic character.By graphic character we mean any character that is printable except the space(‘ ‘) character.Note which character is considered as graphic depend on the locale setting.In “C”(English) locale-also the default locale set in a compiler- all the character within the value 33 to 126 except the space character is considered as graphic character.The declaration of the function is given below.

int isgraph( int c );

Parameters:
c -The character or integer to be tested as graphic character.

Return type
int -Returns non-zero value if ‘c’ is a graphic character.

A code example is given below for “C” locale.

Code example

printf(“%d” , isgraph( ‘>’ ) );

printf(“\n%d” , isgraph( ‘$’ ) );

printf(“\n%d” , isgraph( ‘~’ ) );

printf(“\n%d” , isgraph( ‘[‘ ) );

printf(“\n%d” , isgraph( ‘b’ ) );

printf(“\n%d” , isgraph( ‘ ‘ ) );

printf(“\n%d” , isgraph( 32 ) );

printf(“\n%d” , isgraph( 127 ) );

printf(“\n%d” , isgraph( ‘\xE4’ ) ); //The hexadecimal value ‘xE4’ represent the German character ‘ä’

Output in Code::blocks,

16
16
16
16
16
0
0
0
0

As you can see passing a space character return 0(false) which means it not a graphic character although it is a printable character.For the last line since ‘ä'(xE4) is not part of English alphabet the return value is 0(false).The next section explains briefly on how to set a specific locale.



Testing ‘isgraph’ in different locale

In the previous example we pass a German character to test if it is considered as graphic character in “C” locale and the return value is false.Here let us try setting the locale to German and also pass the same character and see if it is a German graphic character.To set the locale to German call the function ‘setlocale’ and pass the first argument as “LC_CTYPE” and the second argument as “deu_DEU.1252“.The code example is given below.

Include the header locale.h to access the ‘setlocale’ function.

Code example

printf(“%d” , isgraph( ‘\xE4’ ) ); \\xE4=ä

printf(“\n%d” , isgraph( ‘\xFc’ ) ); \\xFC=ü

printf(“\n%d” , isgraph( ‘A’ ) );

printf(“\n%d” , isgraph( ‘a’ ) );

printf(“\n%d” , isgraph( ‘{‘ ) );

printf(“\n%d” , isgraph( ‘@’ ) );

printf(“\n%d” , isgraph( ‘ ‘ ) );

Output in CB,

258
258
257
258
16
16
0

Now we get non-zero value for ‘ä’ , ‘ü’, ‘A’, ‘a’, ‘{‘ and ‘@’ but for the space character ‘ ‘ still false is return.The non-zero return values however are still different for different character,for small letters (‘ä’ , ‘ü’, ‘a’) the function return 258 and for capital letter 257 is return.For other graphic letter 16 is return which is same as the return value when the locale is set to “C” locale;the previous example case.


Related links

->C iscntrl

->C isblank

->C isalpha

->C tolower

->C toupper ctype.h