- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - iswcntrl() 函式
C 的wctype庫 iswcntrl() 函式用於檢查給定的寬字元(型別為 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
廣告