Return: non-zero value if int c is a control character. Otherwise, 0 is returned.

Parameter list: (int
c) - the character that is checked to determine the return value.

Header file:<ctype.h>.

1. So, what is the iscntrl function in C?

The iscntrl function is defined in the Standard Library of C
and
evaluates the received parameter. This function checks if the received parameter is a control character.
Control characters, in turn, encompass ASCII values from 0 to
31 and the last ASCII value - 127.

Given that a passed argument happens to be one of the mentioned ASCII values ([0, 31] or 127),
then the iscntrl function returns a non-zero value. Otherwise,
a zero integer value is returned.

As a way to demonstrate all the ASCII values of control characters in the standard output and make use
of the iscntrl function, we can write the following program
(example 1).

Example 1: Checking result of iscntrl function and printing all the control values.

An accurate implementation of the iscntrl function would need
to firstly check if a received parameter is in the range of [0, 31] or is a 127. And then return a
non-zero or a zero value based on the result. In the code snippet below, you can see such an
implementation.

Example 2: Rewritten iscntrl function in C

intour_iscntrl(intc)

{

if(( c>=0&&c<=31 ) || ( c == 127 ))

return(1);

return(0);

}

In this example, we are checking (on line 3), if the condition is true. In such a case, the our_iscntrl function returns 1 (a non-zero value). If the
condition is false, however, zero is returned.

3. Testing the rewritten iscntrl function

In the code example below, we have set up a simple test that checks if the our_iscntrl function returns the same values as the iscntrl
function from the standard C library.

Example 3: Test of the rewritten iscntrl function in C

#include <stdio.h>

#include <ctype.h>

intour_iscntrl(intc)

{

if(( c>=0&&c<=31 ) || ( c == 127 ))

return(1);

return(0);

}

intmain()

{

inti = 0;

intis_equal = 1;

while(i
<= 127)

{

if(iscntrl(i) > 0 != our_iscntrl(i) > 0)

{

is_equal =
0;

printf("Functions behave differently, when i == (%d)\n", i);

}

i++;

}

if(is_equal)

{

printf("Functions behave the same way!\n");

}

return(0);

}

Possible output:

Functions behave the same way!

In the main function (example 3), we iterate through all the numbers from 0 to 127. With each
iteration, we pass the i variable to both ispunct and our_ispunct
functions (on line 15, example 3). Then we check if return values match (non-zero and
zero). If at any point results do not match, we change the value of the is_equal variable to zero (on line 17, example 3) and
print a message (on line 18, example 3). Only if both functions produce the same results, we
print "Functions behave the same way!" to the standard output (on line 24).

4. Quiz of the reader's knowledge

Quiz : Rewrite C Function (iscntrl)

1Which of the ASCII values is not a control character?

2Which condition checks correctly if a val is a control
character?