English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

Usage and example of C library function iscntrl()

C Standard Library <ctype.h>

The iscntrl() function is used to detect whether a character is a control character (Control Character).
A 'control character' refers to those that have some special function, do not display on the screen, and do not occupy character positions. Characters that cannot be printed on the screen are called control characters. For example, the backspace key, the escape character, the newline character, etc.

Control characters and printable characters are relative, printable characters are those that will be displayed on the screen, and will occupy character positions, 'normal' characters. To check if a character is a printable character, use the isprint() function.

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

This function is inctype.h  Defined in the header file.

Function prototype of iscntrl()

int iscntrl(int argument);

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

When a character is passed as a parameter, the corresponding ASCII value of the character is passed, not the character itself.

Example1Check the specified 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 result

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

Example #2Print all ASCII values of control characters

#include <stdio.h>
#include <ctype.h>
int main()
{
    int i;
    printf("All control character ASCII values are ");
    for (i = 0; i <=127; ++i)
    {
        if (iscntrl(i) != 0) {
            printf("%d ", i);
        }            
    }
    return 0;
}

C Standard Library <ctype.h>