C programming iscntrl ctype.h


In C programming the <ctype.h> iscntrl function test if the argument is a control character.In ASCII table the control character is represented from 0 to 31 and 127.They are the also the control characters in “C locale.For different locale which character is considered as control character will depend on the locale.The declaration of the function is given below.

int iscntrl(int c);

Parameters:
c -The character to be tested for control character.

Return type
int -returns non-zero value if the argument is control character else returns false(0).

Code example

printf(“%d”, iscntrl(0) );

printf(“\n%d”, iscntrl(20) );

printf(“\n%d”, iscntrl(31) );

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

printf(“\n%d”, iscntrl(34) );

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

Output,

32
32
32
32
0
0

For argument 34 and 200 ‘iscntrl’ returns 0;meaning false.

To test for control characters in other locale you can set the locale by calling the function ‘setlocale’ and pass first argument as “LC_CTYPE” macro and the second argument will depend on which locale you wan to set.In Code::blocks,to set the locale to Greek you can pass the second argument as “ell_GRC.1253“.A code example is given below.

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

Code example

setlocale(LC_CTYPE , “ell_GRC.1253”);

printf(“%d”, iscntrl( 0 ) );

printf(“\n%d”, iscntrl( 31 ) );

printf(“\n%d”, iscntrl( 55 ) );

Output,

32
32
0


Related links

->C isblank ctype.h

->C tolower

->C isgraph

->C isxdigit