Using Chinese Chars in an array?
jazzed
Posts: 11,803
Someone has tried using Chinese characters in a char array.
I suspect that the array should be wchar_t and locale set to C.UTF8.
Can someone please describe what it takes to make the program below work?
I'm super busy with bugs otherwise I would dig it myself.
Thanks,
--Steve
I suspect that the array should be wchar_t and locale set to C.UTF8.
Can someone please describe what it takes to make the program below work?
I'm super busy with bugs otherwise I would dig it myself.
Thanks,
--Steve
/** * This is the main Blank Simple Project program file. */ #include <stdio.h> #include "simpletools.h" /* char chi_TextArray[]; */ char eng_Text[] = "Hello There"; char chi_Text[] = "你好吗?"; int main() { pause(500); printf("English: %s\n", eng_Text); pause(500); printf("Chinese: %s\n", chi_Text); pause(1000); }
Comments
Seems to be working fine.
Those "pause" statements on the other hand are an issue, according to the man pages here:
And sure enough putting those back hangs the whole thing.
Eric
It would be great if adding the option -finput-charset=UTF-8 would just solve the problem.
Unfortunately I had to add #include <locale.h> and do setlocale(LC_ALL,""); to make it work with the terminal.
I know it's not a good idea, but if the value for LC_ALL could be used instead of the macro, things would be easier. Objections?
Postedit: Looks like the include is necessary in any event. Seems like -finput-charset=UTF-8 is not necessary then.
If you have to change the source anyway, why not add the include? LC_ALL is almost certainly going to be different on different systems, so it's a good idea to use the symbol rather than the constant.
Eric
Is there a still compelling reason for adding the flag -finput-charset=UTF-8 anyway?
Probably not. GCC usually defaults to UTF-8 input, but I don't know if there is any special handling for Windows (which does not handle UTF-8 well, and insists on using obsolete code pages for 8 bit data).
Eric
Just adding the include seems to fix it. I'll suggest using setlocale() regardless.
There's something wrong here... the include file itself has nothing that would suggest a character encoding or change the behavior of the program.
What exactly is the output if you compile it without the include?
Eric
I've seen some different behaviors now that I can't really explain. I lost my basis.
Now I can't get it to fail unless i use Simple printf which fails without stdio.h but works with stdio.h.
Normal printf seems to work out of the box.