C 庫 - iswprint() 函式



C 的wctypeiswprint()函式用於檢查給定的寬字元(型別為wint_t)是否可以列印。

可列印字元包括數字 (0123456789)、大寫字母 (ABCDEFGHIJKLMNOPQRSTUVWXYZ)、小寫字母 (abcdefghijklmnopqrstuvwxyz)、標點符號 (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)、空格或當前 C 語言區域設定中特有的任何可列印字元。

此函式包括所有圖形字元和空格字元。

語法

以下是iswprint()函式的C庫語法:

int iswprint( wint_t ch );

引數

此函式接受單個引數:

  • ch − 一個待檢查的型別為'wint_t'的寬字元。

返回值

如果寬字元可列印,則此函式返回非零值;否則返回零。

示例 1

以下是一個基本的 C 程式,用於演示 iswprint() 的用法。

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

int main(void) {
   // Set the locale to "en_US.utf8" 
   setlocale(LC_ALL, "en_US.utf8");
   
   wchar_t space = L' ';
   
   // Check if the character is printable
   int res = iswprint(space);
   
   if (res) {
      printf("The character ' ' is a printable character.\n");
   } else {
      printf("The character ' ' is not a printable character.\n");
   }
   return 0;
}

輸出

以下是輸出:

The character ' ' is a printable character.

示例 2

讓我們建立另一個 C 示例,並使用 iswprint() 來識別寬字元集中的可列印字元。

#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
   // printable and non-printable characters
   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 printable character or not
   printf("Checking printable characters:\n");
   for (size_t i = 0; i < num_char; ++i) {
      wchar_t ch = character[i];
      printf("Character '%lc' (%#x) is %s printable char\n", ch, ch, iswprint(ch) ? "a" : "not a");
   }
   
   return 0;
}

輸出

以下是輸出:

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

示例 3

下面的 C 示例使用 iswprint() 來檢查特殊字元在 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 printable character or not
   printf("Checking printable characters:\n");
   for (size_t i = 0; i < num_char; ++i) {
      wchar_t ch = spclChar[i];
      printf("Char '%lc' (%#x) is %s printable char\n", ch, ch, iswprint(ch) ? "a" : "not a");
   }
   
   return 0;
}

輸出

以下是輸出:

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