3

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.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94

2 Answers2

5

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

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

or better yet

wprintf(L"%lc\n", i);
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Thanks you. I've added %lc. Strange, now printf does not print anything. Do you know what could be wrong? – Карина Баринова May 01 '20 at 09:53
  • Can you try with `setlocale(LC_ALL, "");`? it works for me: https://ideone.com/AFX4DK , also, as pointed out by @domsson in comments: check if your console font supports japanese graphs. – David Ranieri May 01 '20 at 09: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 01 '20 at 10:02
  • 2
    It is well explained here: https://www.gnu.org/software/libc/manual/html_node/Setting-the-Locale.html – David Ranieri May 01 '20 at 10:04
  • Still, getting? on the console while running the above programme. Any idea why they are still failing to print the Japanee characters on the screen? – Arun Kumar Aug 11 '22 at 05:36
  • @ArunKumar are you able to print Japanese characters in the console outside a C program?, i.e. with `echo`? It should work if your console supports it. – David Ranieri Aug 11 '22 at 08:15
  • @DavidRanieri Thank you for replying. Not yet, using this https://ideone.com/6sxHdX program to print on the console using MSVC 2017. Getting? on the console screen. I'm trying to validate the characters in the path(Containing Japanese chars) and add the path to the directory list). but failing to do that on windows. – Arun Kumar Aug 11 '22 at 09:54
2

In addition @David Ranieri fine answer, I wanted to explain about the "output gives me only ?????????? 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{|}~
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256