C iscntrl()

The iscntrl() function defined in the ctype.h header file. This function helps to check the specified character is a control character or not. According to the standard ASCII character set, control characters are between ASCII codes 0x00 (NUL), 0x1f (US), and 0x7f (DEL). Also, we can say that control characters are those which cannot be printed on the screen.  For example, backspace, Escape, newline, etc.


int iscntrl(int argument); #where argument will be a character
 

iscntrl() Parameters:

The iscntrl() function takes a single parameter and is in the form of an integer and the return value should be an integer. When a character is passed it is converted into the integer value corresponding to its ASCII value.

Parameter Description Required / Optional
argument  The character to be checked Required

iscntrl() Return Value

 If the given character is a control character, iscntrl() returns a non-zero integer else zero. When the control character is passed we will get a different non-zero integer.

Input Return Value
Zero If the parameter isn't a control character
Non zero number If the parameter is a control character

Examples of iscntrl()

Example 1: How to check control character in C?


#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    int output;

    ch = 'A';
    output = iscntrl(ch);
    printf("If %c is passed to iscntrl() = %d\n", ch, output);

    ch = '\n';
    output = iscntrl(ch);
    printf("If %c is passed to iscntrl() = %d", ch, output);

    return 0;
}
 

Output:


If A is passed to iscntrl() = 0
If
 is passed to iscntrl() = 1

Example 2: Passing an integer out of range


#include <stdio.h>
#include <ctype.h>

int main()
{
    int x;

    printf("The ASCII value of all control characters are:\n");
    for (x=0; x<=127; ++x)
    {
        if (iscntrl(x)!=0)
            printf("%d ", x);
    }
    return 0;
}
 

Output:


The ASCII value of all control characters are:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127