c doesn't print “┌──┐” character correctly

橙三吉。 提交于 2019-12-22 17:38:41

问题


Good afternoon, I'm facing a problem on my c code and I don't know what is causing it.

Every time I try to print characters like these: "┌──┐" my program simply prints some strange characters, like on this screenshot:

I'm using Qt Creator on Windows, with Qt version 5.5.0 MSVC 64 bits. The compiler is the Microsoft Visual C++ Compiler 12.0 (amd64).

I tried changing the locale but with no success. The only way I found to print these characters was to define them as int variables with the ASCII code and printing them, but it led to some really extensive and ugly coding, like this:

int cSupEsq = 218;  //'┌'
int cSupDir = 191;  //'┐'
int cInfEsq = 192;  //'└'
int cInfDir = 217;  //'┘'
int mVert = 179;    //'│'
int mHor = 196;     //'─'
int espaco = 255;   //' '
int letraO = 111;   //'o'

//Inicia limpando a tela da aplicação
clrscr();

//Linha 1
printf("%c", cSupEsq);
for (i = 1; i < 79; i++) { printf("%c", mHor); }
printf("%c", cSupDir);

Is there any way I can make the program treat these characters correctly? What could be causing this problem?


回答1:


Your solution to use the OEM code points is the right way to go, codepage 850/437 is the default code page for the console and therefore should work. You could also use SetConsoleOutputCP to ensure the correct code page is used for the console.

Having said that, what is happening when you do not use your workaround is that the source file is being saved using a different codepage ie. not codepage 850/437. The in memory representation of the source code is Unicode (probably UTF-8), when you save the file the in memory representation of the characters are mapped to the target code page for the file.

What you can do is to save the file using the 850/437 codepage as the target, I don't know how you do this in Qt Creator (If you can at all), in Visual Studio for example you can select the down arrow on the Save button and select "Save with encoding", you can then proceed to select the target codepage, in your case code page 850. This will ensure that the in memory code points are mapped correctly to the file to be compiled.

I hope that helps explain the issue.




回答2:


It shouldn't be necessary to print the characters one at a time. Instead, you can use an escape sequence:

printf("\xDA\xBF\xC0\xD9\xB3\xC4\xFF");


来源:https://stackoverflow.com/questions/33904089/c-doesnt-print-character-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!