C 库 - iswcntrl() 函数



C 的wctypeiswcntrl() 函数用于检查给定的宽字符(类型为 wint_t)是否为控制字符。控制字符是不打印的字符,而是控制文本处理和输出设备的行为。

控制字符包括换行符(\n)、回车符(\r)以及ASCII控制字符代码范围 (0x00-0x1F 和 0x7F) 中的其他字符,以及当前区域设置特有的任何控制字符。

其中,0x00-0x1F 是 ASCII 表中的前 32 个字符。0x7F 是“del”(删除)字符。

语法

以下是 iswcntrl() 函数的 C 库语法:

int iswcntrl( wint_t ch );

参数

此函数接受单个参数:

  • ch − 一个待检查的 'wint_t' 类型的宽字符。

返回值

如果宽字符是控制字符,则此函数返回非零值;否则返回零。

示例 1

以下是一个基本的 C 示例,演示了 iswcntrl() 函数的使用。

#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;
}

输出

以下是输出:

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

示例 2

我们创建一个 C 程序,并使用 iswcntrl() 来检查 Unicode 字符“换行符”在默认区域设置中是否为控制字符。

#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;
}

输出

以下是输出:

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

示例 3

下面的 C 程序检查默认区域设置和 Unicode 区域设置中的控制字符。

#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;
}

输出

以下是输出:

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
广告