Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cout" fails to work with Chinese characters

My codes are as simple as these:

#include <iostream>
using namespace std;
//Some codes here...
bool somefunction(){
    cout<<"单元格";
    return false;
}

and this is what I got:

error C2143: syntax error: missing ';' before 'return';
error C2001: newline is constant;

Moreover, if i change "单元格" into an English version like "cell", it works perfectly;

like image 857
Elderry Avatar asked Dec 18 '25 03:12

Elderry


1 Answers

The compiler errors indicate that your compiler doesn't support Unicode characters in source code. You'll have to escape them, use wide-character constants, and wcout:

wcout << L"\x5355\x5143\x683c";

If you need to output characters in a specific encoding (e.g. gb2312), use that encoding in the string literal:

cout << "\xb5\xa5\xd4\xaa\xb8\xf1"; // string encoded with GB2312
like image 174
nneonneo Avatar answered Dec 20 '25 15:12

nneonneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!