skip to Main Content

When I run this simple program on Linux (Ubuntu):

int main() {
        const char* s = "olé";

        std::cout << s << std::endl;
}

I get:

olé

Now contrast the same program on Windows:

int main() {
        LPCSTR s = "olé";      
        std::cout << s << std::endl;
}

This gives me:

olé

I think I can understand why this could be so e.g., after reading this. The surprise comes when I try to run following program (on Windows):

int main() {   
        LPCWSTR s = L"olé";
std::wcout << s << std::endl;
}

Note the use of std::wcout in above. I would expect this to print the original string olé but instead I get:

olΘ

Can anyone explain:

  1. Why?

  2. Is there any way to get C++ to print the original string on Windows?

2

Answers


  1. In the 1st example, your source file is likely encoded in UTF-8, so your literal gets saved as-is in the executable as UTF-8, but your terminal on Windows is not configured to display UTF-8. Whereas on Linux, it likely is.

    In the 2nd example, your source file is likely encoded in UTF-8, but you are probably not telling the compiler to parse the file as UTF-8, so the literal does not get setup correctly.

    Login or Signup to reply.
  2. You have to call an obscure Microsoft function to set your stdout to UTF16 as shown here – https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170

    The following few lines, built in VS2022 as console app, reproduce your issue. Uncommenting the _setmode call fixes it:

    #include <windows.h>
    #include <iostream>
    #include <fcntl.h>
    #include <io.h>
    
    int main() 
    {
        //_setmode(_fileno(stdout), _O_U16TEXT);
        std::wcout << L"olén";
        return 0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search