What's the difference between glib gunichar an

2019-04-29 14:18发布

问题:

I'm trying to write some C code which is portable only so far as the user has gcc, and has glib installed.

From all my research, I've found that with gcc, a wchar_t is always defined as 4 bytes, and with glib a gunichar is also 4 bytes.

What I haven't figured out is if like a gunichar, a wchar_t is encoded as UCS4 as well. Is this the case? If so, I should be able to simply cast a gunichar* to a wchar_t* and use the stdc wcs* functions, right?

回答1:

If you use GLib, don't use wchar_t. Use its unicode support, it's a lot better than the C standard library's support.

wchar_t is 4 bytes on Linux and Mac OS (and a few others), not on Windows (it's 2 bytes there) and some others. Portable code means avoiding wchar_t like the plague.