0

Why does

printf("%s", "米"); outputs and returns 3, but

printf("%ls", L"米"); prints nothing and returns -1 ?

I'm confused here.

Willy
  • 763
  • 2
  • 8
  • 29
  • Which compiler are you using? – Eugene Sh. Mar 02 '15 at 22:43
  • I type 'gcc', but since I run OSX 10.9, I think it's clang by default now – Willy Mar 02 '15 at 22:45
  • 2
    As this thread suggests: http://stackoverflow.com/questions/15528359/printing-utf-8-strings-with-printf-wide-vs-multibyte-string-literals , in order to use the wide character notation (`L""` along with `%ls`) the locale has to be set correctly. – Eugene Sh. Mar 02 '15 at 22:47
  • Ok i feel dumb now. Thank you sir ! – Willy Mar 02 '15 at 22:50
  • 3
    For the first: If the text is stored as UTF8 in your *source*, all that `printf` does is send the literal 3 bytes to the console. Then your *console* interprets the UTF8 sequence again, and shows the correct character. – Jongware Mar 02 '15 at 22:51
  • there are versions of printf just for printing wide characters. for instance: 'wprintf()' the code should use those facilities for printing wide characters. – user3629249 Mar 02 '15 at 23:03

0 Answers0