0% found this document useful (0 votes)
90 views

Printing Japanese Characters in C Program - Stack Overflow

The document is a question on Stack Overflow asking how to print Japanese characters in a C program. The question provides code that converts Unicode values for Japanese characters to decimal and prints them using printf, but only gets question marks as output. Responses suggest using %lc instead of %c to print wide characters, and that setting the locale to the empty string with setlocale(LC_ALL, "") fixes the issue. They also note that the console font may need to support Japanese characters.

Uploaded by

Fabio C
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Printing Japanese Characters in C Program - Stack Overflow

The document is a question on Stack Overflow asking how to print Japanese characters in a C program. The question provides code that converts Unicode values for Japanese characters to decimal and prints them using printf, but only gets question marks as output. Responses suggest using %lc instead of %c to print wide characters, and that setting the locale to the empty string with setlocale(LC_ALL, "") fixes the issue. They also note that the console font may need to support Japanese characters.

Uploaded by

Fabio C
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stack Overflow sign up log in

Questions Jobs Tags Users Badges Ask

2 Printing Japanese characters in C program


c unicode setlocale

I want to print Japanese characters using the C program. I've found the Unicode range of
some Japanese characters, converted them to decimal and used the for loop to print them:

setlocale(LC_ALL, "ja_JP.UTF8");
for (int i = 12784; i <= 12799; i++) {
printf("%c\n",i);
}

locale.h and wchar.h are present in the header.

The output gives me only ?????????? characters.

Please let me know how it could be resolved.

Share Improve this question Follow

Карина Баринова asked



163 12 May 1 '20 at 9:34

David Ranieri edited


● ●
35.2k 6 44 ● 84 May 1 '20 at 10:10

2 Does your font include Japanese characters? – domsson May 1 '20 at 9:38

Add a comment

2 Answers order by votes

%c is only able to print characters from 0 to 127, for extended characters use:
4

printf("%lc\n", i);

or better yet

wprintf(L"%lc\n", i);

Share Improve this answer Follow

David Ranieri answered


● ●
35.2k 6 44 ● 84 May 1 '20 at 9:42
Thanks you. I've added %lc. Strange, now printf does not print anything. Do you know what could be wrong? –  Карина
Баринова May 1 '20 at 9:53

Can you try with setlocale(LC_ALL, ""); ? it works for me: ideone.com/AFX4DK , also, as pointed out by
@domsson in comments: check if your console font supports japanese graphs. – David Ranieri May 1 '20 at 9:56

setlocale(LC_ALL, "") has fixed the issue. Thank you very much! The last question. I thought it was needed to specify
what locale I want to use, so I used ja_JP.UTF8. I'm just interested why setlocale(LC_ALL, "") works as well. –  Карина
Баринова May 1 '20 at 10:02

2 It is well explained here: gnu.org/software/libc/manual/html_node/Setting-the-Locale.html – David Ranieri May 1 '20


at 10:04

Add a comment

In addition @David Ranieri fine answer, I wanted to explain about the "output gives me only
2
?????????? characters."

"%c" accepts an int argument. Recall a char passed to a ... function is converted to an
int . Then

the int argument is converted to an unsigned char , and the resulting character is
written. C17dr § 7.21.6.1 8.

Thus printf("%c" ... handles values 0-255. Values outside that range being converted to
that range.

OP's code below re-written in hex.

// for (int i = 12784; i <= 12799; i++) {


for (int i = 0x31F0; i <= 0x31FF; i++) {
printf("%c\n",i);
}

With OP locale setting and implementation, printing values [0xF0 - 0XFF] resulted in '?' . I am
confident that is true for [0x80 - 0xFF] for OP. Other possibilities exist. I received � .

Had OP done the below, more familiar output would be seen, though not the Hiragana
characters desired.

for (int i = 0x3041; i <= 0x307E; i++) {


printf("%c",i);
}

ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Share Improve this answer Follow


chux - Reinstate Monica answered

111k 11 ● 102 ● 207 May 1 '20 at 15:33

Your Answer

Body

Add picture

Log in

OR

Name

Email

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
2021 Stack Exchange, Inc. user contributions under cc by-sa

You might also like