C iscntrl() Function Tutorial

  • Post author:
  • Post category:C

In this section, we will learn what the iscntrl() function is and how to use it in C.

Control Characters in C

Those types of non-printable characters that cause an effect on a text are called control-characters.

For example, newline, backspace, escape character, etc.

C iscntrl() Function

The `iscntrl()` function is used to check whether a character is a control character.

The prototype of the function exists in the `ctype.h` header file and we need to include this header file in order to use ` iscntrl()` function.

iscntrl() Function Syntax

Here’s the prototype of the ` iscntrl()` function:

int iscntrl (int argument);

iscntrl() Function Parameters

This function takes one argument and that is the character that we want to check.

iscntrl() Function Return Value

The return value of the function is:

  • 0: If the character was not a control-character.
  • Positive value: if the character was in fact a control-character.

Example: using iscntrl() function in C

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

int main() {

    char character = '\0';
    char c2 = '5';

    printf("The first character: %d \nThe second character:%d", iscntrl (character), iscntrl (c2));

    return  0;
}

Output:

The first character: 32

The second character:0

The first character in this example is the newline character and so the result of calling the ` iscntrl ` function is a positive value because `\0` is a control-character. But the result of the second character is 0 because the actual value of the character is `5` and it’s not a control-character.

Leave a Reply