How do I print a Chinese or Japanese character (non-English) in C?
wchar *wc = (wchar *)calloc(50,sizeof(wchar*));
wcscpy(wc ,L"john 麥克風");
printf(" wc : %S \n",wc);
I got john
only in my output.
How do I print a Chinese or Japanese character (non-English) in C?
wchar *wc = (wchar *)calloc(50,sizeof(wchar*));
wcscpy(wc ,L"john 麥克風");
printf(" wc : %S \n",wc);
I got john
only in my output.
Software doesn't know about "characters". All it knows are numbers which happen to be interpreted as characters by whatever other software actually displays your output. (For your software, a
is merely an alias for 0x61. Turning that 0x61 into the combination of pixels recognized as a
is up to your terminal, your GUI or whatever.)
Handling of non-ASCII characters in your source is implementation-defined, i.e. it's up to your compiler what to do with the Chinese characters in your source. If you want to play it safe (and portable), you will have to write non-ASCII characters using their encoding values, e.g. by using \x
notation. (Or the newer \u
, but that doesn't make the point as neatly as it already refers to Unicode explicitly.)
It should occur to you at this point that you have to agree with your program about what encoding you are using, or your numerical values would mean something entirely different to your program. This is done by setting the locale appropriately. (There are the encodings ISO-8859-1 and ISO-8859-15 and EBCDIC and ... for single-byte, UTF-8 and UTF-16 and EUC and ISO-2022-JP and ... for multi-byte, UCS-2 and UTF-32 and probably others for wide... while everything should be Unicode IMHO, it clearly isn't, and even without the exotics there are three very different Unicode encodings out there.)
As others already pointed out, if you're doing "wide" output, you should use "wide" output functions. (Be aware that "multibyte" is yet another type of fish entirely, and that there are 8-bit multibyte encodings -- UTF-8 -- as well as 16-bit multibyte encodings -- UTF16...)
Whatever you are printing your output to (e.g. the terminal, GUI) needs to support your locale / encoding, and needs to have a font at its disposal that actually has glyphs (i.e. printable characters) loaded for the associated code points. (Otherwise you will get "?", some other placeholder, or some funny looking stuff with tiny hexcodes in it.)
While your actual problem most likely rests with using printf()
instead of wprintf()
, all the above points must work together in order to correctly print non-ASCII characters. Hence, you might have other problems as well, but it's hard to tell.
Make sure the terminal is using a UTF-8 encoding. C++ itself is unicode agnostic. Also you will need to use std::wcout
or wprintf
to print those.
This works for me:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
void main()
{
setlocale(LC_ALL, "");
wprintf(L"Why? 为什么?\n");
}
I solved this problem by setlocale(LC_ALL, "zh_CN.UTF-8")
for Chinese and setlocale(LC_ALL, "ja_JP.UTF-8")
for Japanese on Ubuntu.
Or you can just modify your system default locale info.