2

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:

screenshot of my terminal output

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?

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
JefersonFG
  • 83
  • 2
  • 10

2 Answers2

3

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.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • 1
    Code page 437 doesn't contain `Ô`, it's more likely code page 850. Your assumption that the source is in UTF-8 appears very likely, either that or the compiler is translating it. – Mark Ransom Nov 25 '15 at 03:55
  • @MarkRansom, you are correct I had forgotten about 850. Thanks I will update the response. – Chris Taylor Nov 25 '15 at 05:27
  • You were right the first time though, code page 437 *is* the default for many of us. Just not everybody. – Mark Ransom Nov 25 '15 at 12:47
  • It should be possible to use escape characters, shouldn't it? Wouldn't that be more reliable? – Harry Johnston Nov 25 '15 at 23:39
0

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");
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158