C programming isprint ctype.h


In C programming the <ctype.h> isprint function test if the character is printable.By printable we simple mean the character occupy space in the output medium.Which character is considered as printable will depend on the current locale setting.In “C” locale-the default locale-the character from 32 to 126 is considered as printable,note here even space(32) is considered as printable.The declaration of the function is shown below.

int isprint(int c);

Parameters:
c -The character to be tested is printable.

Return type
int -Return non-zero value if the argument is printable else false.

The space character is not considered as graphic character so isgrpah(‘ ‘) return false.

Link : C isgraph
Code example

printf(“%d” , isprint(‘A’) );

printf(“\n%d” , isprint(‘*’) );

printf(“\n%d” , isprint( 56 ) );

printf(“\n%d” , isprint( 200) );

printf(“\n%d” , isprint(‘\xDC’) ); //represent the German letter ‘Ü’

Output,

1
16
4
0
0

The German letter ‘Ü’ is not part of “C” locale and also 200 does not represent any printable character so ‘isprint’ returns false.If you look at the non-zero value return for ‘A’ , ‘*’ and 56,they are all unique value.It seems for upper case Latin Alphabets [A,Z] the function return 1,for any character that is not Latin Alphabet upper case or lower character and also does not represent digit in ASCII table the function return 16,the integer 56 map to the digit ‘8’ in ASCII table which means for all digit [0,9],’isprint’ returns 4 and lastly you can verify yourself for lower case Latin character which the function most probably return 2.



Testing ‘isprint’ in the locale besides the “C” lcoale

What if we set the locale to say,German,in this case all the character set which is considered printable in German locale will test true by ‘isprintf’.To set the locale to German call the function ‘setlocale’ and pass the first argument as “LC_CTYPE” macro and the second argument as “deu_DEU.1252”.The second argument will change depending on the locale you want to set.And note the string “deu_DEU.1252” for German locale will work only in Code::Blocks or any IDE that uses MinGW compiler,so Visual Studio and the like which uses different compiler will not accept the second argument.Check the compiler documentation for the acceptable locale string.

Code example

setlocale(LC_CTYPE , “deu_DEU.1252”);

printf(“%d” , isprint(‘A’) );

printf(“\n%d” , isprint( 32 ) ); //32 represent the space in ASCII

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

printf(“\n%c” , isprint( ‘\D6’ ) ); //represent the German character ‘Ö’

printf(“\n%d” , isprint(‘\xDC’) ); //represent the German letter ‘Ü’

Output in CB,

257
64
16
257

Now you can see after setting the locale to German the German Alphabet letter are accepted as printable.


Related links

->C isgraph

->C islower

->C isblank

->C iscntrl