"Output" with special characters

Asked

Viewed 152 times

4

I am using the GCC compiler version 5.3 for the following program, written in Aptana:

#include <stdio.h>

void main(void)
{
    int positivo = 32767;
    int negativo = -32768;

    printf("%d +1 é %d\n", positivo, positivo+1);
    printf("%d -1 é %d\n", negativo, negativo-1); 
}

With the following output:

tela com resultado

Already in the text block, with the following:

tela com resultado

How should I proceed to have the characters displayed correctly ?

  • Probably compatibility problems of terminal "encodings", text editors... (in which environments are you?)

  • Thank you for your attention, what do you mean by "environments"? @Jjoao

  • In windows, command line, you can define which charset to use (example windows-CP1252, Unicode-utf8, etc.) in the text editors you can select the carser/encoding (example ansi, etc.). Under linux and mac settings have more reasonable starting values but are also configurable. Suggestion to switch everything to the same charset/encoding -- maybe Unicode-uft8.

1 answer

2


Probably need to use a setlocale() to allow the use of accented characters.

#include <stdio.h>
#include <locale.h>

int main(void) {
    int positivo = 32767;
    int negativo = -32768;
    setlocale(LC_ALL,"Portuguese");
    printf("%d +1 é %d\n", positivo, positivo + 1);
    printf("%d -1 é %d\n", negativo, negativo - 1); 
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But it is no advantage because they already worked without the setlocale(). I couldn’t test in his condition to reproduce the problem.

  • This solution works perfectly in the notepad, already in Aptana he exchanged for another, also special... And I also noticed, that you exchanged void for main and yet specified a type, can say why ?

  • You probably need to configure the text editor to write with another encoding, one of them is writing wrong, but that’s another problem.

  • And what was the reason for the exchanges you made ?

  • Set the character encoding, only you have to record all your fonts with the same, otherwise it turns crazy, every time you save the font in a different editor, you have to change the code to handle this, which is a complete nonsense.

  • @bigown, it seemed to me that this is essentially a matter of encoding and its compatibility between editor and terminal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.