c - 应该如何正确使用 wctype.h 函数?

标签 c language-lawyer ctype

ctype.h 中的各种 is... 函数(例如 isalphaisdigit)不是完全可以预测。它们采用 int 参数,但期望字符值在 unsigned char 范围内,因此在 char 已签名的平台上,传递一个 char 值直接可能会导致不需要的符号扩展。我相信处理此问题的典型方法是首先显式转换为 unsigned char

好的,但是处理 wctype.h 中的各种 isw... 函数的正确、可移植的方法是什么? wchar_tchar 一样,也可以是有符号或无符号的,但是因为 wchar_t 本身就是一个 typedef,一个类型名unsigned wchar_t 是非法的。

最佳答案

这不就是吗wint_t是为了? iswXxxxx()函数采用 wint_t输入:

ISO 9899:1999 的各个部分都涵盖了这一点,逆向计算:

§7.25 Wide character classification and mapping utilities <wctype.h>

§7.25.2.1.1 The iswalnum function

Synopsis

#include <wctype.h>
int iswalnum(wint_t wc);

Description

The iswalnum function tests for any wide character for which iswalpha or iswdigit is true.

§7.24 Extended multibyte and wide character utilities <wchar.h>

§7.24.1 Introduction:

wint_t

which is an integer type unchanged by default argument promotions that can hold any value corresponding to members of the extended character set, as well as at least one value that does not correspond to any member of the extended character set (see WEOF below);269)

269) wchar_t and wint_t can be the same integer type.

“默认参数提升不变”应该意味着它必须与 int 一样大, 虽然它可能是 shortunsigned short如果sizeof(short) == sizeof(int) (现在这种情况很少见,尽管对于某些 16 位系统来说确实如此)。

§7.17 Common definitions <stddef.h>

wchar_t

which is an integer type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales; the null character shall have the code value zero and each member of the basic character set shall have a code value equal to its value when used as the lone character in an integer character constant.

只要把值传给iswalnum()或其亲属是有效的 wchar_t或 WEOF,该函数将正常工作。如果您凭空制造了值并设法弄错了值,则会出现未定义的行为。

关于c - 应该如何正确使用 wctype.h 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468306/

相关文章:

c - 有什么方法可以锁定互斥量并让它在给定时间后自动解锁?

c - C 中的 GLUT 屏幕截图

c - 如何编写宏以返回逗号和整数或什么都不返回

连续分配与顺序分配

c - 如何实现<ctype.h>?

c++ - isblank() 无法识别

c++ - 我终于在链表中创建了我的添加函数,但不能在 main 中使用。 :(

c++ - g++ 和 clang++ 都存在模板函数参数包扩展问题?

c++ - C++ 标准是否允许对具有 const 成员的 POD 对象进行零初始化?

c - 从文件中的单词中删除字母 C - 有问题的符号