1

I want to store a string with characters from extend ascii table, and print them. I tried:

wchar_t wp[] = L"Росси́йская Акаде́мия Нау́к ";
printf("%S", wp);

I can compile but when I run it, nothing is actually displayed in my terminal.

Could you help me please?

Edit: In response to this comment:

wprintf(L"%s", wp);

Sorry, I forgot to mention that I can only use write(), as was only using printf for my first attempts.

indiv
  • 17,306
  • 6
  • 61
  • 82
edelangh
  • 311
  • 1
  • 12
  • 1
    Describe, *"nothing is happening"*? Does your code compile? does it run? Do other parts of the program work properly? – abelenky Dec 12 '14 at 16:34
  • 1
    `wprintf(L"%s", wp);` – Jabberwocky Dec 12 '14 at 16:34
  • do `fflush(stdout);` afterwards. – nos Dec 12 '14 at 16:40
  • > Describe, "nothing is happening"? My bad, I was a bit imprecise, I can compile but when I run it, nothing is actually displayed in my terminal. wprintf(L"%s", wp); Sorry, I forgot to mention that I can only use write(), as was only using printf for my first attempts. – edelangh Dec 12 '14 at 17:09

2 Answers2

1

If you want wide chars (16 bit each) as output, use the following code, as suggested by Michael:

wprintf(L"%s", wp);

If you need utf8 output, you have to use iconv() for conversion between the two. See question 7469296 as a starting point.

Community
  • 1
  • 1
Kai Petzke
  • 2,150
  • 21
  • 29
  • As I tell before i cant use wprintf, I have to reimplement %S option of printf with only write and va_arg. – edelangh Dec 12 '14 at 17:30
  • Then use iconv() to convert your wide string to UTF-8, and use write() to output it. (Assuming, that you need to create UTF-8 output). – Kai Petzke Dec 12 '14 at 17:37
1

You need to call setlocale() first and use %ls in printf():

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

int main(int argc, char *argv[])
{
    setlocale(LC_ALL, "");
    // setlocale(LC_ALL, "C.UTF-8"); // this also works

    wchar_t wp[] = L"Росси́йская Акаде́мия Нау́к";
    printf("%ls\n", wp);

    return 0;
}

For more about setlocale(), refer to Displaying wide chars with printf

Franklin Fu
  • 53
  • 1
  • 4