C++ iscntrl()

The iscntrl() function in C++ checks if the given character is a control character or not.

iscntrl() prototype

int iscntrl(int ch);

The iscntrl() function checks if ch is a control character or not as classified by the currently installed C locale. By default, the characters with the codes from 0x00 to 0x1F and 0x7F are considered control characters.

There are 32 control characters in the ASCII character set, including null, line feed, start of text, backspace, tab etc.

The behaviour of iscntrl() is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.

It is defined in <cctype> header file.

iscntrl() Parameters

ch: The character to check.

iscntrl() Return value

The iscntrl() function returns non zero value if ch is a control character, otherwise returns zero.

Example: How iscntrl() function works

#include <cctype>
#include <iostream>

using namespace std;

int main()
{
    char ch1 = '\t';
    char ch2 = 'x';

    iscntrl(ch1)?cout << ch1 << " is a control character":cout << ch1 << " is not a control character";
    cout << endl;
    iscntrl(ch2)?cout << ch2 << " is a control character":cout << ch2 << " is not a control character";
        
    return 0;
}

When you run the program, the output will be:

	 is a control character
x is not a control character