iscntrl() Function

The iscntrl() function in C checks if a given character is a control character. Control characters are those that do not occupy a printing position on a display, which is the opposite of what is determined by isprint(). This function is essential for distinguishing non-printable characters in text processing.


Syntax of iscntrl()

</>
Copy
int iscntrl(int c);

Parameters

ParameterDescription
cAn integer representing the character to be checked, usually cast from a char or EOF.

Note that for the standard ASCII character set used in the default “C” locale, control characters are those with ASCII codes from 0x00 (NUL) to 0x1F (US) and the DEL character (0x7F). This function does not consider printable characters.

Return Value

The function returns a nonzero value (true) if the character is a control character, and zero (false) if it is not.


Examples for iscntrl()

Example 1: Checking a Newline as a Control Character

This example demonstrates how iscntrl() identifies the newline character as a control character.

Program

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

int main() {
    int ch = '\n';

    if (iscntrl(ch)) {
        printf("The character is a control character.\n");
    } else {
        printf("The character is not a control character.\n");
    }

    return 0;
}

Explanation:

  1. The program assigns the newline character '\n' to ch.
  2. iscntrl() checks whether ch is a control character.
  3. Since newline is a control character, the program prints a confirmation message.

Program Output:

The character is a control character.

Example 2: Checking a Printable Character

This example shows how iscntrl() handles a typical printable character.

Program

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

int main() {
    int ch = 'A';

    if (iscntrl(ch)) {
        printf("The character is a control character.\n");
    } else {
        printf("The character is not a control character.\n");
    }

    return 0;
}

Explanation:

  1. The character 'A' is assigned to ch.
  2. The iscntrl() function checks if 'A' is a control character.
  3. Since 'A' is a printable character, the function returns false and the program indicates that it is not a control character.

Program Output:

The character is not a control character.

Example 3: Scanning a String for Control Characters

This example demonstrates how to scan through a string and identify control characters using iscntrl().

Program

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

int main() {
    char str[] = "Hello\nWorld\t!";
    int i = 0;

    while (str[i] != '\0') {
        if (iscntrl(str[i])) {
            printf("Character at index %d ('\\x%02X') is a control character.\n", i, str[i]);
        } else {
            printf("Character at index %d ('%c') is not a control character.\n", i, str[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A string "Hello\nWorld\t!" is initialized, containing both printable and control characters (newline and tab).
  2. The program iterates through each character of the string.
  3. For each character, iscntrl() checks whether it is a control character.
  4. The program prints an appropriate message along with the index of the character.

Program Output:

Character at index 0 ('H') is not a control character.
Character at index 1 ('e') is not a control character.
Character at index 2 ('l') is not a control character.
Character at index 3 ('l') is not a control character.
Character at index 4 ('o') is not a control character.
Character at index 5 ('\x0A') is a control character.
Character at index 6 ('W') is not a control character.
Character at index 7 ('o') is not a control character.
Character at index 8 ('r') is not a control character.
Character at index 9 ('l') is not a control character.
Character at index 10 ('d') is not a control character.
Character at index 11 ('\x09') is a control character.
Character at index 12 ('!') is not a control character.