那是因为wchar_t
需要持有WEOF
值和char
不需要持有EOF
value.
For char
, the fputc()
, putc()
and putchar()
functions need to accept values which can hold both values in the unsigned char
and EOF
range, where EOF
can be a negative number so a int
is required to hold them both.1
Whereas wchar_t
itself is required to hold a WEOF
character as well as the biggest locale.2 WEOF
represents a value which fits inside wchar_t
but doesn't overlap with any locale.3
This is made more confusing because of the the names of char
and wchar_t
, you shouldn't see wchar_t
as a char
but more as a int
which size isn't dependent on the architecture but on the size of the biggest locale (and on the value of WEOF
).4
1 Why putchar, toupper, tolower, etc. take a int instead of a char?
2 Quoting ISO/IEC 9899:201x 7.19.2:
WEOF
它扩展为类型的常量表达式wint_t
其值不对应于扩展字符集的任何成员。本子条款中的几个函数接受(并返回)它以指示文件结尾,也就是说,不再有来自
溪流。它还用作不对应于扩展字符集的任何成员的宽字符值。
3 Quoting your link:
宏观WEOF
计算结果为类型的常量表达式wint_t
其值不同于扩展字符集的任何成员。
4 Quoting C++ Reference:
wchar_t
其值范围可以表示受支持的语言环境中指定的最大扩展字符集的所有成员的不同代码的类型。
In c++, wchar_t
是一个独特的基本类型(因此它没有在<cwchar>
也没有任何其他标头)。
In c, 这是一个typedef
属于整数类型。