Understanding and writing wchar_t in C

10,975

A wchar_t is similar to a char in the sense that it is a number, but when displaying a char or wchar_t we don't want to see the number, but the drawn character corresponding to the number. The mapping from the number to the characters aren't defined by neither char nor wchar_t, they depend on the system. So there is no difference in the end usage between char and wchar_t except for their sizes.

Given the above, the most trivial implementation of printf("%ls") is one where you know what are the system encodings for use with char and wchar_t. For example, in my system, char has 8 bits, has encoding UTF-8, while wchar_t is 32 bits and has encoding UTF-32. So the printf implementation just converts from UTF-32 to UTF-8 and outputs the result.

A more general implementation must support different and configurable encodings and may need to inspect what's the current encoding. In this case functions like wcsnrtombs() or iconv() must be used.

Share:
10,975

Related videos on Youtube

kRYOoX
Author by

kRYOoX

Updated on June 04, 2022

Comments

  • kRYOoX
    kRYOoX almost 2 years

    I'm currently rewriting (a part of) the printf() function for a school project. Overall, we were required to reproduce the behaviour of the function with several flags, conversions, length modifiers ...

    The only thing I have left to do and that gets me stuck are the flags %C / %S (or %lc / %ls).

    So far, I've gathered that wchar_t is a type that can store characters on more than one byte, in order to accept more characters or symbols and therefore be compatible with pretty much every language, regardless of their alphabet and special characters.

    However, I wasn't able to find any concrete information on what a wchar looks like for the machine, it's actual length (which apparently vary based on several factors including the compiler, the OS ...) or how to actually write them.

    Thank you in advance

    Note that we are limited in the functions we are allowed to use. The only allowed functions are write(), malloc(), free(), and exit(). We must be able to code any other required function ourselves.

    To sum this up, what I'm asking here is some informations on how to interpret and write "manually" any wchar_t character, with as little code as possible so that I can try to understand the whole process and code it myself.

  • ninjalj
    ninjalj over 9 years
    Actually, if __STDC_ISO_10646__ is defined, wchar_t should store Unicode codepoint values, as of the date specified in that macro. See ISO C 6.10.8.2
  • hdante
    hdante over 9 years
    And if STDC_ISO_10646 is not defined, then wchar_t need not store Unicode codepoint values.
  • kRYOoX
    kRYOoX over 9 years
    This is pretty much what I guessed based on @Mgetz comment to my question. Thank you for confirming it though. With some more reading on Unicode encoding and how to manipulate it, I was able to implement what I needed.