Inconsistency in definitions of fputwc(), putwc()

2019-02-26 15:05发布

问题:

Why fputwc(), putwc() and putwchar() take argument of type wchar_t instead of wint_t? This contradicts corresponding non-wide character functions fputc(), putc() and putchar(), which take int, not char.

回答1:

That is because wchar_t is required to hold an WEOF value and char is not required to hold an 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 which expands to a constant expression of type wint_t whose value does not correspond to any member of the extended character set. It is accepted (and returned) by several functions in this subclause to indicate end-of-file, that is, no more input from a stream. It is also used as a wide character value that does not correspond to any member of the extended character set.

3 Quoting your link:

The macro WEOF evaluates to a constant expression of type wint_t whose value is different from any member of the extended character set.

4 Quoting C++ Reference:

wchar_t Type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales.

In c++, wchar_t is a distinct fundamental type (and thus it is not defined in <cwchar> nor any other header).

In c, this is a typedef of an integral type.



标签: c glibc wchar-t