C Library - iscntrl() function



The C ctype library iscntrl() function is used to check whether a given character is a control character. Control characters are non-printable characters that are used to control the operation of devices (such as printers or displays) and include characters like newline, tab, and others.

Syntax

Following is the C library syntax of the iscntrl() function −

int iscntrl(int ch);

Parameters

This function accepts a single parameters −

  • ch − This is the character to be checked, passed as an integer. The value should be representable as an unsigned char or be equal to EOF.

Return value

The iscntrl() function returns a non-zero value (true) if the character is a control character. Otherwise, it returns zero (false).

Example 1: Checking a newline character

This example checks if the newline character ('\n') is a control character. The function returns true, confirming that it is.

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

int main() {
   char ch = '\n'; // newline character

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

Output

The above code produces following result −

The character '\n' is a control character.

Example 2: Checking a printable character

Here we check if the character 'A' is a control character. The function returns false, indicating that 'A' is not a control character.

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

int main() {
   char ch = 'A'; // printable character

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

Output

After execution of above code, we get the following result −

The character 'A' is not a control character.
Advertisements