4

I am trying to print an emoji on standard output console in C++ on Mac OS X environment – clang.

This first code works correctly:

#include <iostream>
#include <cwchar>

int main(int argc, const char *argv[]){
  char myEmoji[4] = "⛩";
  std::cout << "emoji example: " << myEmoji << std::endl;
  return 0;
}

and on console I can see:

./emoji ; exit
emoji example: ⛩
logout

When I try this, it works unexpectedly for me:

#include <iostream>
#include <cwchar>

int main(int argc, const char *argv[]){
  wchar_t myEmoji = L'⛩';
  std::wcout << "emoji example: " << myEmoji << std::endl;
  return 0;
}

and this time I get:

./emoji ; exit
emoji example: logout

Where I am wrong?

shogitai
  • 1,823
  • 1
  • 23
  • 50
  • Well, I wouldn't stick non-ASCII characters in a source file to begin with. Use escape sequences. – PaulMcKenzie Apr 14 '16 at 21:34
  • Your is a good advice – surely – but nothing of illicit: for example [here](http://stackoverflow.com/questions/2493785/how-i-can-print-the-wchar-t-values-to-console) and in many other ones. – shogitai Apr 14 '16 at 22:02
  • 1
    Can you rig up a version that works, using, say, `std::wstring` inline aggregate-initialized with the emoji? Doing that will likely tell you where the problem is in your program – which is either a) the storage, assignment and management of wide-character data, or b) an issue with the `std::wout` instance of`std::wostream` (which of all of the STL internals, the stream I/O freaks me out the most and I have no idea how it works; there are many subtle options hidden in layers of classes and subclasses therein). – fish2000 Jul 05 '17 at 05:45

0 Answers0