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.
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.2WEOF 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
1Why putchar, toupper, tolower, etc. take a int instead of a char?
2 Quoting ISO/IEC 9899:201x 7.19.2:
WEOFwhich expands to a constant expression of typewint_twhose 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.
The macro
WEOFevaluates to a constant expression of typewint_twhose value is different from any member of the extended character set.
wchar_tType 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_tis a distinct fundamental type (and thus it is not defined in<cwchar>nor any other header).In c, this is a
typedefof an integral type.
来源:https://stackoverflow.com/questions/39655296/inconsistency-in-definitions-of-fputwc-putwc-and-putwchar-in-glibc