0

Consider:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    wstring str = L"こんにちは";
    wcout << str << endl;
    system("pause");
}

I am trying to print Japanese (hello ) from a C++ program, but I am getting an error. I have saved this program in Notepad using Unicode encoding and then compiled it using MinGW 4.7.2, but I get the following error:

cd "E:\GCC test"
g++ -c unicode.cpp

Output:

unicode.cpp:1:1: error: stray '\377' in program
unicode.cpp:1:1: error: stray '\376' in program
unicode.cpp:1:1: error: stray '#' in program
unicode.cpp:3:4: error: invalid preprocessing directive #i
unicode.cpp:5:4: error: invalid preprocessing directive #i
unicode.cpp:1:5: error: 'i' does not name a type
unicode.cpp:11:2: error: 'i' does not name a type
9
  • 4
    1an error’, eh? What error?! 2 Unicode is not an encoding. Commented Mar 21, 2016 at 8:03
  • E:\GCC test>g++ -c unicode.cpp unicode.cpp:1:1: error: stray '\377' in program unicode.cpp:1:1: error: stray '\376' in program unicode.cpp:1:1: error: stray '#' in program unicode.cpp:3:4: error: invalid preprocessing directive #i unicode.cpp:5:4: error: invalid preprocessing directive #i unicode.cpp:1:5: error: 'i' does not name a type unicode.cpp:11:2: error: 'i' does not name a type Commented Mar 21, 2016 at 8:12
  • 1
    @Biffens: if unicode is not an encoding then what is option we get while saving a text file on windows in NOTEPAD under encoding, it says :ANSI, UNICODE,UNICODE BIG ENDIAN and UTF-8?? Commented Mar 21, 2016 at 8:24
  • Have you tried UTF-8 (the best encoding there is (if you ask me))? Commented Mar 21, 2016 at 8:27
  • 1
    @M.M u8"" and friends are supposed to solve that, but I've come across at least one compiler that didn't conform. Commented Mar 21, 2016 at 9:24

1 Answer 1

1

By the errors it looks like you've got a file in UTF-16LE with a BOM (Byte Order Mark), and that the compiler doesn't like that.

\377\376 = 0xfffe = a UTF-16LE BOM

Try removing the BOM, and/or try a different encoding. UTF-8 is an excellent encoding that doesn't need a BOM, and that most compilers and a lot of other tools will understand.


As for Unicode, it is not a binary character encoding. There are a few encodings that are ‘tied to’ Unicode, however. UTF-8 and UTF-16 are probably the most common such encodings.

If an editor offers to save a file in ‘Unicode encoding’, then try to stay away from that editor. If that editor is Notepad, then there are more reasons to stay away from it. Get yourself a proper editor for programming, one that understands encodings and EOLs, and that has syntax highlight, etc.

Sign up to request clarification or add additional context in comments.

6 Comments

i have tried with UTF-8 it got compiled and created an executable but nothing is getting printed to console.
Once again; That seems like an entirely different issues, so post a new question.
@Vickey Nothing or do you see unintelligible characters getting printed? If it's the latter, then your terminal's font doesn't have glyphs for Japanese characters. Try pasting the Japanese "hello" directly in the prompt e.g. > echo 'こんにちは' and see if it prints fine.
you have to jump through hoops to get Windows console applications to display unicode. You have to set codepage in the console, select a monospace font with the right glyphs (by editing the registry), and do the right thing in the program source. (For GUI programs it is easy though, Windows API supports unicode)
@legends2k: my console is not recognizing こんにちは these japanese character when i do echo こんにちは....what to do??? help !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.