C iscntrl()

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 the 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:\n");
    for (i=0; i<=127; ++i)
    {
        if (iscntrl(i)!=0)
            printf("%d ", i);
    }
    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
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community