Open In App

Program and syntax for iscntrl(int c) function in C

Last Updated : 31 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In C, iscntrl() is a predefined function used for string and character handling. ctype is the header file required for character functions. A control character is one that is not a printable character i.e, it does not occupy a printing position on a display.

This function is used to check if the argument contains any control characters. There are many types of control characters in C++ such as:

  1. Horizontal tab- ‘\t’
  2. Line feed- ‘\n’
  3. Backspace- ‘\b’
  4. Carriage return- ‘\r’
  5. Form feed- ‘\f’
  6. Escape
  7. Alert Character- ‘\a’

The iscntrl() function checks whether a character passed to the function as an argument is a control character or not. If the character passed is a control character, then the function returns a non-zero integer i.e. the ASCII value of the corresponding character. If not, it returns 0.
According to the standard ASCII character set, control characters are between ASCII codes 0x00 (NUL), 0x1f (US), and 0x7f (DEL). Specific compiler implementations may define additional control characters for many specific platforms in the extended character set (above 0x7f).

Syntax: 

int iscntrl(int c);

Parameters:
c- This is the character to be checked.

Return Value:
This function returns a non-zero value i.e. the ASCII value of the character if c is a control character, else it returns 0.

Applications:

1. Given a string, print the string till the first control character is encountered.

Algorithm:

  1. Traverse the given string character by character and write the characters to the standard output using putchar().
  2. Break the loop when a control character is found in the string.
  3. Print the final string from the standard output.

Examples:

Input: char str1[] = "GeeksforGeeks \t is good";
Output: geeksforgeeks

Input: char str2[] = "Computer programming\n is best";
Output: Computer programming

C




// C program to implement 
// the above approach
#include <stdio.h>
#include <ctype.h>
  
// Driver code
int main () 
{
  int i = 0, j = 0;
  char str1[] = "geeksforgeeks \t is good";
  char str2[] = "is best \n point";
  
  // Prints string till control character \a 
  while(!iscntrl(str1[i])) 
  {
    putchar(str1[i]);
    i++;
  }
  
  // Prints string till control character \n 
  while(!iscntrl(str2[j])) 
  {
    putchar(str2[j]);
    j++;
  }
  
  return(0);
}


Output

geeksforgeeks is best 

2. Given a string, traverse the string from the beginning and check if the character encountered is a control character or not.

Algorithm:

  1. Traverse the given string character by character up to its full length and check if the character is a control character.
  2. If it is a control character, increment the counter value by 1, else move to the next character.
  3. Print the value of the counter.

Examples:

Input: string1[]= "GeeksforGeeks  \n is \n best platform \for Computer Science"
Output: 3

Input: string2[]= "C is a \n powerful programming language."
Output: 1

C




// C program to implement
// the above approach
#include <stdio.h>
#include <ctype.h>
  
// Driver code
int main()
{
  char c;
  int result;
  c = 'R';
  result = iscntrl(c);
  printf("if %c is passed to iscntrl() = %d\n"
          c, result);
  
  c = '\n';
  result = iscntrl(c);
  printf("if %c is passed to iscntrl() = %d"
          c, result);
  
  return 0;
}


Output

if R is passed to iscntrl() = 0
if 
 is passed to iscntrl() = 2

3. Given a string, print ASCII values of all control characters.

Algorithm:

  1. Traverse the given string character by character up to its full length and check if the character is a control character.
  2. If it is a control character, print the ASCII value of the control character.
  3. Print till the value does not equal zero. (using for loop or while loop).

Examples:

Input: 
for (i = 0; i <= 50; ++i)
{
    if (iscntrl(i) != 0)
    printf("%d ", i);
}
Output:
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

Input:
for (i = 0; i <= 127; ++i)
{
    if (iscntrl(i) != 0)
    printf("%d ", i);
}
Output:
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

C




// C program to implement
// the above approach
#include <stdio.h>
#include <ctype.h>
  
// Driver code
int main()
{
  int i;
  printf("The ASCII value of all control characters are:\n");
    
  for (i = 0; i <= 50; ++i)
  {
    if (iscntrl(i) != 0)
      printf("%d ", i);
  }
    
  return 0;
}


Output

The ASCII value of all control characters are:
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 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads