Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a Chinese character?

Tags:

c

printf

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.

like image 962
user3336737 Avatar asked Mar 20 '14 07:03

user3336737


People also ask

Is there an app to scan Chinese characters?

Which of these Chinese character recognition apps is your perfect fit? A beginner will be much more comfortable with an app like Pleco, which does more than just your average translations. A fluent speaker might simply need a little help once in a while by using WeChat's “scan to translate” feature.

How do you type the Chinese symbol?

On the “Keyboards and Languages” tab, click on “Change Keyboards” > “Add” > “Chinese.” 4. Expand the option of “Chinese” and then expand the option “Keyboard.” Select the keyboard layout marked as “Chinese.” You can ignore other keyboard layouts. Click “OK” and then “Apply.”

How do I view Chinese characters in PDF?

In Acrobat in Windows, you must install the Asian language support files by using the custom installation and selecting the Asian Language Support options under Create Adobe PDF and View Adobe PDF.

Can UTF 8 handle Chinese characters?

UTF-8 is a character encoding system. It lets you represent characters as ASCII text, while still allowing for international characters, such as Chinese characters. As of the mid 2020s, UTF-8 is one of the most popular encoding systems.


2 Answers

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.)

  1. 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.)

  2. 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.)

  3. 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...)

  4. 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.

like image 146
DevSolar Avatar answered Sep 18 '22 22:09

DevSolar


It's a wide string, so you need wprintf:

like image 39
The Archetypal Paul Avatar answered Sep 20 '22 22:09

The Archetypal Paul