C 库 - iswgraph() 函数



C 的wctypeiswgraph() 函数用于检查给定的宽字符(类型为 wint_t)是否为图形字符。图形字符包括所有可打印字符,但空格除外。

可打印字符包括数字(0123456789)、大写字母(ABCDEFGHIJKLMNOPQRSTUVWXYZ)、小写字母(abcdefghijklmnopqrstuvwxyz)、标点符号(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)或当前 C 语言环境特有的任何图形字符。

语法

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

int iswgraph( wint_t ch );

参数

此函数接受单个参数:

  • ch - 它是要检查的类型为 'wint_t' 的宽字符。

返回值

如果宽字符具有图形表示字符,则此函数返回非零值,否则返回零。

示例 1

以下是演示如何使用iswgraph() 在宽字符集中识别图形字符的基本 C 语言示例。

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

int main(void) {
   // "en_US.utf8" to handle Unicode characters
   setlocale(LC_ALL, "en_US.utf8");

   // Array of wide characters including
   wchar_t character[] = {L'A', L' ', L'\n', L'\u2028'};
   
   size_t num_char = sizeof(character) / sizeof(character[0]);

   // Check and print if a character is graphical character or not
   printf("Checking graphical characters:\n");
   for (size_t i = 0; i < num_char; ++i) {
      wchar_t ch = character[i];
      printf("Character '%lc' (%#x) is %s graphical char\n", ch, ch, iswprint(ch) ? "a" : "not a");
   }
   
   return 0;
}

输出

以下是输出:

Checking graphical characters:
Character 'A' (0x41) is a graphical character
Character ' ' (0x20) is a graphical character
Character '
' (0xa) is not a graphical character
Character ' ' (0x2028) is not a graphical character

示例 2

让我们创建另一个 C 程序,并使用iswgraph() 检查特殊字符在 Unicode 语言环境中是否为图形字符。

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

int main(void) {
   // "en_US.utf8" to handle Unicode characters
   setlocale(LC_ALL, "en_US.utf8");

   wchar_t spclChar[] = {L'@', L'#', L'$', L'%'};
   
   size_t num_char = sizeof(spclChar) / sizeof(spclChar[0]);

   // Check and print if a character is graphical character or not
   printf("Checking graphical characters:\n");
   for (size_t i = 0; i < num_char; ++i) {
      wchar_t ch = spclChar[i];
      printf("Char '%lc' (%#x) is %s graphical char\n", ch, ch, iswprint(ch) ? "a" : "not a");
   }
   
   return 0;
}

输出

以下是输出:

Checking graphical characters:
Char '@' (0x40) is a graphical char
Char '#' (0x23) is a graphical char
Char '$' (0x24) is a graphical char
Char '%' (0x25) is a graphical char
c_library_wctype_h.htm
广告