C library function iscntrl( )

In this tutorial, you will learn about c library function iscntrl() which is a character handling function used to check whether the character supplied as the argument is a control character or not.

C library function iscntrl( )

As discussed earlier, we have to include ctype.h header file for using iscntrl( ) standard character library function.

#include <ctype.h>

C library function iscntrl( ): Function prototype


int isxdigit( int ch);

This function checks whether its argument is a contorl character or not.

List of control characters in C


Whitespace character Description
‘ \a’ alert
‘\f’ form feed
‘\n’ newline
‘\r’ carriage return
‘\t’ horizontal tab
‘\v’ vertical tab
‘\b’ back space

iscntrl returns a true value if its argument is one of the above control character and zero (0) otherwise.

[adsense1]

Example of C iscntrl( )


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

int main()
{
   printf("Demonstration of iscntrl():\n");
   printf("\n%s%s%s", "New line", iscntrl('\n') ? " is a " : " is not a ", "control character");
   printf("\n%s%s%s", "Back space", iscntrl('\b') ? " is a " : " is not a ", "control character");
   printf("\n%s%s%s", "percentage", iscntrl('%') ? " is a " : " is not a ", "control character");

   return 0;
}

Output

Demonstration of iscntrl():
New line is a control character
Back space is a control character
percentage is not a control character

Explanation
In the above example, standard character library function iscntrl determines whether its argument is a control character or not.

Conditional operator ?: does the selection between two strings "is a" and "is not a".

This can be simply done with the help of isspace and if...else statement.

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

int main()
{
   printf("Demonstration of iscntrl( ):\n");

   if( iscntrl('\n') == 0)
     printf("\nNew line is not control character");
   else
     printf("\nNew line is a control character");

   if( iscntrl('\b') == 0)
     printf("\nBack space is not control character");
   else
     printf("\nBack space is a control character");

   if( iscntrl('%') == 0)
     printf("\nPercentage is not control character");
   else
     printf("\nPercentage is a control character");

 return 0;
}

Output

Demonstration of iscntrl():
New line is a control character
Back space is a control character
Percentage is not a control character

Explanation

In this example, we have used if...else statements instead of a conditional operator.

Here, if...else statement does the selection between two strings.