开发者

C programming, unicode and the linux terminal

So what I'm trying to do is write Japanese characters to my terminal screen using C and wide characters.

The question is whats wrong with what I'm doing so that I can fix it, what other caveats should I expect while using wide characters and do you have any other comments about what I'm trying to do?

The bad code:

#include <stdio.h>
#include <wchar.h>

int main( ) {
    wprintf(L"%c\n", L"\x3074");
}

This doesn't work, but I want to know why.

the problem only gets worse when I try to use a wchar_t to hold a value:

wchar_t pi_0 = 0x3074;      // prints a "t" when u开发者_运维问答sed with wprintf
wchar_t pi_1 = "\x3074";    // gives compile time warning
wchar_t pi_2 = L"\x3074";   // gives compile time warning

So I'd also like to make this work too, as I plan on having data structures holding strings of these characters.

Thanks!


The type of "\x3074" is const char[] and the type of L"\x3074" is const wchar_t[].

If you need a wchar_t, use single quotes:

L'\x3074'

Also %c prints a char, but for wchar_t you need a %lc.


There are at least two problems in the code.

  • the first one has been pointed out by Kenny, the format doesn't match the argument
  • the second one is that you miss a call to setlocale()

(There is also the assumption that the wide character set is Unicode -- I seem to remember it is always the case for Linux, but it isn't universal).

In a correctly configured terminal,

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main( ) {
    setlocale(LC_ALL, "");
    wprintf(L"%ls\n", L"\x0152\x3074");
    return 0;
}

should work. If it doesn't, I would start by checking the result of setlocale() and wprint().

(I've added U+0152 which is the OE ligature so that I can check the behavior; I'm not using a font which has U+3074)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜