How to use iscntrl function in C programming?

The iscntrl function in C programming checks whether the argument passed is a control character or not. In the default, “C” locale, the control characters are the characters with the codes 0x00-0x1F and 0x7F.

It is declared in ctype.h and takes one argument in the form of integer and returns the value of type int. If the character passed is a control character, it returns a non-zero integer. If not, it returns 0.

Syntax of iscntrl function in C:

//Syntax of iscntrl

int iscntrl(int c);

Parameters:

c => character to classify

Return value:

Non-zero value => If the argument is a control character (like backspace, Escape, newline, etc)
0 => If the argument is neither a control character.

 

Example,

Input : a
Output : Zero


Input : '\n'
Output : Non-zero value


Input : @
Output : Zero

 

C Program to Check whether a Character Entered by User is Control character or not Using the iscntrl:

Let’s see a C program to check given character is a control character or not.

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

int main()
{
    unsigned char c = 'A';;

    int result = iscntrl(c);

    result ? printf("A is control char\n"):printf("A is not a control char\n");

    c = '@';
    result = iscntrl(c);
    result ? printf("@ is control char\n"):printf("@ is not a control char\n");

    c = '\n';
    result = iscntrl(c);
    result ? printf("\\n is control char\n"): printf("\n is not a control char\n");

    return 0;
}

Output:

iscntrl function in c

 

Explanation:

As we know that iscntrl() returns a non-zero value for the control character. So when we pass ‘A‘ and ‘@‘ to the iscntrl it returns zero because both are not a control characters. But when we pass '\n' it returns a non-zero value and prints the message that it is a control character.

 

Print ASCII value of All Control characters:

Many beginners and freshers don’t know the ASCII value of control characters in the default “C” locale. So here I am writing a small program that helps you to find the ASCII value of control characters in decimal. If you want to print it in hex, you need to change the format specifiers from %d to %x.

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

int main()
{
    int c;

    printf("Decimal ASCII value of all \
control characters are in C locale:\n\n\n");

    for (c=0; c<=127; ++c)
    {
        //iscntrl check control character
        if (iscntrl(c)!=0)
        {
            printf("%d ", c);
        }
    }
    return 0;
}

Output:

Decimal ASCII value of all control characters are in C locale:


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127

 

 

Print Count of the control characters in given input string:

There are many applications of iscntrl in C programming. But to find out the count of control characters in a given input stream is very popular. So let’s see a C code to find the count of the control characters in the given input stream.

Algorithm:

1. Traverse the given string character by character and passed it into the iscntrl function.

2. Increment the counter variable whenever encounter the control character.

3. Break the loop when encountered the null character (Limitation there must not be another null character in the string except the terminating null character).

4. Return the value of the counter from the function and print the value in the main function.

 

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

unsigned int findCountControlCharGivenStream(char *str)
{
    unsigned int counter = 0;
    if (str != NULL)
    {
        unsigned int i = 0;
        // counting of control char
        while (str[i] != '\0')
        {
            if (iscntrl((unsigned char)str[i]))
            {
                ++counter;
            }
            ++i;
        }
    }
    // returning total number of control char
    // present in given input stream
    return (counter);
}

int main()
{
    char str[] = "aticleworld\t.com";

    unsigned int counter = findCountControlCharGivenStream(str);

    printf("Total number of char in input stream is : %u\n\n", strlen(str));

    printf("\nNumber of control char in the "
           "given input stream is : %u\n\n", counter);

    return 0;
}

Output:

Total number of char in input stream is : 16

Number of control char in the given input stream is : 1

 

Print the string till the first control character is encountered:

Another popular program is to print given a string till the first control character is encountered. With the help of iscntrl()we can easily do it. So let’s see a small program.

Algorithm:

1. Traverse the given string character by character and passed it into the iscntrl function.

2.  If the character is not a control character it returns zero. You can see in the while loop braces, I have used logical Not ( ! ) with iscntrl(). So for each non-control character loop will run and print the character on stdout.

3 When the control character will encounter the while loop will break.

 

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

int main()
{
    char str[] = "aticleworld\t.com";
    unsigned int i = 0;

    // printing till first control char
    while (!iscntrl((unsigned char)str[i]))
    {
        putchar(str[i]);
        ++i;
    }
    return 0;
}

Output:

aticleworld

 

Note: If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of iscntrl is undefined.

 

As we know the behavior of iscntrl is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.

int my_iscntrl(char ch)
{
    return iscntrl((unsigned char)ch);
}

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *