Here's a nice C puzzle this evening.
-
Here's a nice C puzzle this evening.
What type is a L'c' wide character constant?
Answer: wchar_t.
What type does the fprintf '%lc' format take?
Answer: wint_t.
So you need to cast your wide character when printing it with the conversion specifier which accepts wide characters.
Also, shout-out to gcc for msp430 where wchar_t is 32 bits (long int) and wint_t is 16 bits (unsigned int). I'm sure there's a story there.
-
Here's a nice C puzzle this evening.
What type is a L'c' wide character constant?
Answer: wchar_t.
What type does the fprintf '%lc' format take?
Answer: wint_t.
So you need to cast your wide character when printing it with the conversion specifier which accepts wide characters.
Also, shout-out to gcc for msp430 where wchar_t is 32 bits (long int) and wint_t is 16 bits (unsigned int). I'm sure there's a story there.
@keithp isn't wint_t supposed to be at least as wide as wchar_t since it must be able to store any wchar_t plus WEOF?
-
@keithp isn't wint_t supposed to be at least as wide as wchar_t since it must be able to store any wchar_t plus WEOF?
@oblomov C23 says wint_t can hold any anything in the extended character set + WEOF (7.31.1) and that wchar_t holds distinct codes for everything in the largest extended character set among the supported locales (7.21).
So they're similar, but I haven't found anything requiring wint_t to hold any wchar_t value. It seems "obvious", but then MSVC claims to be compliant while using 16 bits for these types.
C is slippery; perhaps someone else will find more conclusive wording than me.
-
undefined oblomov@sociale.network shared this topic
-
@oblomov C23 says wint_t can hold any anything in the extended character set + WEOF (7.31.1) and that wchar_t holds distinct codes for everything in the largest extended character set among the supported locales (7.21).
So they're similar, but I haven't found anything requiring wint_t to hold any wchar_t value. It seems "obvious", but then MSVC claims to be compliant while using 16 bits for these types.
C is slippery; perhaps someone else will find more conclusive wording than me.
@keithp my go-to for these questions is cppreference, and
https://en.cppreference.com/w/cpp/string/wide.html#Types
defines wint_t as
«wint_t
integer type that can hold any valid wide character and at least one more value
(typedef)»whereas for wchar_t
https://en.cppreference.com/w/cpp/language/types.html#Character_types
wchar_t — type for wide character representation [...]
so wint_t should never be smaller than wchar_t, if my reading is correct.