Search code examples
c++netbeansg++mingwspecial-characters

How to print out ≠ to terminal using g++?


I want to print in the terminal. I tried

cout << '\u2248' << endl;
cout << '\U00002248' << endl;
cout << '≠' << endl;

which gives

14846344
14846344
14846368

I tried replacing the single quotes with double

Ôëê
Ôëê
Ôëá

How can it be done? I'm curious what the explanation for the output I'm getting is? I'm running Netbeans 9 but have tested directly from command line with g++ too. I think this should be possible because echo ≠ produces the correct output in the Windows command prompt.


Solution

  • So, in C++, like in plain C, by default we can work just with ASCII characters. Char variables contains just 8 bits(1 byte) to store values so maximum - 2^8=256 different symbols can be coded by one char variable. Single quotes (like 'a') are storing char variables so inside of them can be placed just ASCII-character. Your character is not a part of ASCII table and we need to change the encoding.

    For just print(not store/process) your character, you should use another encoding such as UTF-8. You can do it programmatically:

    std::setlocale(LC_ALL, /*some system-specific locale name, probably */ "en_US.UTF-8");
    std::cout << "\u2260" << std::endl;
    

    Or via command line options to g++ (such as -finput-charset=UTF-16). As you can see, I'm using double quotes to print non-ASCII symbols to console.