C library - iswcntrl() function



The C wctype library iswcntrl() function is used to check whether a given wide character (of type wint_t) is a control character. Control characters are characters that do not print but instead control the behaviour of text processing and output devices.

The control character include characters such as newline (\n), carriage return (\r), and others in the ASCII control character code range (0x00-0x1F and 0x7F) and any control character specific to the current locale.

Where, 0x00-0x1F These are the first 32 character in the ASCII table. and 0x7F is the "del" (delete) character.

Syntax

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

int iswcntrl( wint_t ch );

Parameters

This function accepts a single parameter −

  • ch − It is a wide character of type 'wint_t' to be checked.

Return Value

This function returns Non-zero value if the wide character is a control character, zero otherwise.

Example 1

The following is the basic c example that demonstrate the use of iswcntrl() function.

#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
int main() {
   // new line character
   wchar_t ch1 = L'\n';
   // regular character
   wchar_t ch2 = L'A';

   if (iswcntrl(ch1)) {
      wprintf(L"'%lc' is a control character.\n", ch1);
   } else {
      wprintf(L"'%lc' is not a control character.\n", ch1);
   }

   if (iswcntrl(ch2)) {
      wprintf(L"'%lc' is a control character.\n", ch2);
   } else {
      wprintf(L"'%lc' is not a control character.\n", ch2);
   }
   return 0;
}

Output

Following is the output −

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

Example 2

We create a c program and using the iswcntrl() to the check if the Unicode character "line separator" is a control character in the default locale.

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
   // Define the Unicode character "line separator"
   wchar_t ch = L'\u2028'; 
   // Check and print if the character is a control character in the default locale
   printf("In the default locale, iswcntrl(%#x) = %d\n", ch, iswcntrl(ch));
   
   return 0;
}

Output

Following is the output −

In the default locale, iswcntrl(0x2028) = 0

Example 3

The below c program checks the control character in both the default and Unicode locale.

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main (void){
   // Array of wide characters including control and non-control ch
   wchar_t characters[] = { L'\n', L'\t', L'A', L'\u2028', L'\u00A0' };

   size_t num_characters = sizeof (characters) / sizeof (characters[0]);
   
   // Check and print if each character is a control character in the default locale
   printf ("Checking characters in the default locale:\n");
   for (size_t i = 0; i < num_characters; ++i) {
      wchar_t ch = characters[i];
      printf ("Character '%#x' (", ch);
      if (iswcntrl (ch)) {
         printf ("control character");
      } else {
         printf ("not a control character");
      }
      printf (")\n");
   }
   // Set the locale to "en_US.utf8"
   setlocale (LC_ALL, "en_US.utf8");
   
   // Check and print if each character is a control character in the Unicode locale
   printf ("\nChecking characters in Unicode locale (en_US.utf8):\n");
   for (size_t i = 0; i < num_characters; ++i) {
      wchar_t ch = characters[i];
      printf ("Character '%#x' (", ch);
      if (iswcntrl (ch)) {
         printf ("control character");
      } else {
         printf ("not a control character");
      }
      printf (")\n");
   }
   return 0;
}

Output

Following is the output −

Checking characters in the default locale:
Character '0xa' (control character)
Character '0x9' (control character)
Character '0x41' (not a control character)
Character '0x2028' (not a control character)
Character '0xa0' (not a control character)

Checking characters in Unicode locale (en_US.utf8):
Character '0xa' (control character)
Character '0x9' (control character)
Character '0x41' (not a control character)
Character '0x2028' (control character)
Character '0xa0' (not a control character)
c_library_wctype_h.htm
Advertisements