0

Sample code:

int main(int argc, char *argv[]){
    int x = sizeof(wchar_t);
    printf("%i\n",x);
    wchar_t t = 0x011E;
    printf("%c\n\n",t);
    wchar_t a = 0x0041;
    printf("%c\n\n",a);
}

which will print:

4


A

0x011E is the UTF-16 code for the character ğ. Why is it not printed?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • 1
    I believe you need `%lc` for a `wchar`. You may need to use `wprintf`, too. – Jon Skeet Apr 21 '15 at 15:41
  • First: the conversion specification `"%c"` expects a value of type `char`, not a value of type `wchar_t`. You are invoking Undefined Behaviour by passing values of the wrong type to `printf()`. Also you need to `#include ` for the correct prototype for `printf()` – pmg Apr 21 '15 at 15:41
  • 1
    It may depend upon your environment (notably operating system, compiler, etc.) Often, the terminal (e.g. on most Linux) is UTF-8 encoded. – Basile Starynkevitch Apr 21 '15 at 15:42
  • @JonSkeet wprintf("%lc\n\n",t); did not solve it for me. – Koray Tugay Apr 21 '15 at 15:47
  • @pmg I have the include statement I did not show it here. – Koray Tugay Apr 21 '15 at 15:47
  • @KorayTugay: besides the `"%lc"`, you also need a `setlocale` call, to set the proper locale. – ninjalj Apr 21 '15 at 18:58

1 Answers1

0

When you print a char you actually just send it's code to console. and console renderer interprets this code with its own character table. so you have to use the same encoding in your source code and in console.

If you are runing windows, see here how to change encoding in cmd.exe.

Community
  • 1
  • 1
light_keeper
  • 627
  • 5
  • 15