r/cprogramming • u/kolorcuk • Mar 25 '26
Unicode printf?
Hello. Did or do you ever use in professional proframming non char printf functions? Is wprintf ever used?
char16, char32 , u8_printf, u16_printf, u32_printf ever used in actual programs?
I am writing a library and i wonder how actually popular are wide and Unicode strings in the industry. Does no one care about it, or, specifically about formatting output are Unicode printf functions actually with value? For example why not just utf8 with standard printf and convert to wider when needed?
5
Upvotes
13
u/reallyserious Mar 25 '26
Computers are used in Asia too. So yes, unicode is the standard.
14
u/LeeHide Mar 25 '26
wstring/wprintf and so on are NOT about Unicode. You can encode Unicode just fine with UTF-8, all of it. You don't need 16 bit chars. 16 bit chars are also not Unicode. If you have 16 bit chars (wide chars), put Unicode characters in it, and then e.g. split the string by indexing, you absolutely do not end up with valid unicode by any guarantee.
If you want Unicode, use a Unicode library and stick to UTF-8.
5
u/WittyStick Mar 25 '26
"Wide characters" in C should be considered a legacy feature. They're an implementation-defined type which varies between platforms. On Windows a
wchar_tis 16-bits (UCS-2), and on SYSV platformswchar_tis 32-bits.The behavior of
wchar_tdepends on the current locale - it does not necessarily represent a Unicode character.New code should use
char8_tfor UTF-8,char16_tfor UTF-16 andchar32_tfor UTF-32.Most text today is Unicode, encoded as UTF-8 or UTF-16 (Windows/Java). UTF-32 is rarely used for transport or storage, but is a useful format to use internally in a program when processing text.
2
u/EpochVanquisher Mar 25 '26
The main purpose of wprintf is Windows compatibility. If you want to print to the Windows console, wprintf is a good choice there. The rest of the Windows API makes heavy use of wchar_t. There are ways to avoid it, but wchar_t is usually preferred.
In most other cases, it is a mistake to use wchar_t. But it is critically important on Windows.
2
u/Relative_Bird484 Mar 25 '26
On Windows it’s actually the default. While the Win32-API is available in ANSI and wide, the ANSI-Versions are just wrappers that convert to wide and invoke the wide-version.
The native NtAPI uses wide characters only, albeit with an own Pascal-inspired string format (dedicated length field instead of 0-termination).
3
u/First-Golf-8341 Mar 25 '26
I have used only UTF-8 and found that sufficient.
1
u/Tau-is-2Pi Mar 25 '26
Yes, mostly on Windows. 8-bit strings are still using language-dependent encodings instead of UTF-8 by default over there.
1
u/tomysshadow Mar 26 '26 edited Mar 26 '26
It doesn't really matter if other people use them, they're available for your use, the important question is whether they are suitable for your use case.
If I just want to, say, print a log message that is an entirely constant message where I know it's all ASCII with no special characters, then I wouldn't bother with prefixing the string with L"..." to make it wide to go out of my way to use wprintf. I would default to a narrow string most of the time, especially if you're on a platform that doesn't natively use wide strings at all. The reason that these functions exist is if you already have a wide string lying around from some API that returns them and want to print it out, or you want to print a Unicode string on a platform that assumes char* means the string isn't Unicode.
For Windows programming in particular, it is generally assumed that a wide string (
wchar_t*) is Unicode while a narrow string (char*) is an ASCII string in the current codepage (read: the encoding of the string depends on the system locale,) because this is the interpretation of the WinAPI A/W functions. This is why Windows programmers typically use wide strings because it guarantees that WinAPI will interpret the string the same way everywhere and it'll be able to use characters not in the current codepage. But crucially this assumption does not need to always be upheld; it's perfectly valid to have a UTF-8 encoded narrow string in your program for your own use, or for use with some other library expecting UTF-8.So a
char*can be Unicode, in the UTF-8 encoding, just as awchar_t*can be Unicode, in the UTF-16 encoding. It's just that the WinAPI "A" functions (like CreateFileA) are not guaranteed to read the string correctly if it's UTF-8, because they expect the string to be in the current codepage (which could be Windows-1252 for example.)Note that it is possible for the current codepage to be UTF-8 (and modern Windows versions have been encouraging this,) in which case the WinAPI "A" functions will indeed interpret a char* as Unicode UTF-8. But this is not always a guarantee.
This has implications for printf/wprintf because under the hood they likely call a WinAPI function, meaning that printf will expect the string to be in the current codepage, which may not be UTF-8. Despite this, I still use narrow strings most of the time, because so long as you're not using special characters (you're just using A to Z and basic punctuation) that string will be interpreted the same regardless of codepage. It's only when you're dealing with more exotic stuff like accents that the top bit is potentially set and then it's more of a concern. At least for constant strings.
If on the other hand you are, say, asking the user to enter a filename then I wouldn't assume, I'd use a wide string. Because then it's possible the string could end up containing any character at runtime