Join our newsletter for the latest updates.

C iscntrl()

The iscntrl() function checks whether a character is a control character or not.

Characters that cannot be printed on the screen are known as control characters.  For example, backspace, Escape, newline etc.

The iscntrl() function checks whether a character (passed to the function) is a control character or not. If character passed is a control character, it returns a non-zero integer. If not, it returns 0

This function is defined in ctype.h header file.


Function Prototype of iscntrl()

int iscntrl(int argument);

The isntrl() function takes a single argument and returns an integer.

When character is passed as an argument, corresponding ASCII value of the character is passed instead of that character itself.


Example #1:  Check control character

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

int main()
{
    char c;
    int result;

    c = 'Q';
    result = iscntrl(c);
    printf("When %c is passed to iscntrl() = %d\n", c, result);

    c = '\n';
    result = iscntrl(c);
    printf("When %c is passed to iscntrl() = %d", c, result);

    return 0;
}

Output

When Q is passed to iscntrl() = 0
When 
 is passed to iscntrl() = 1

Example #2: Print ASCII value of All Control characters

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

int main()
{
    int i;

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