3

When I compile a code with a text in Spanish the accents always show like weird symbols. The file encoding is UTF-8 and the editor is configured in Spanish also.

The code:

#include "iostream"
using namespace std;
int main(){
    printf("niño á é í ó ú");
    return 0;
};

Output in the terminal:

ni├▒o ├í ├® ├¡ ├│ ├║

enter image description here

Please tell me if it is possible to change this, it's too annoying.

I've change the file encoding and the editor language and nothing works.

OS: Windows

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 3
    Post code as text not as image – Jason Apr 01 '23 at 02:43
  • Could you maybe give us the exact output of the following code, so that we can see exactly what data values are being sent to the terminal/console? `char test[] = "niño á é í ó ú"; for ( size_t i = 0; i < sizeof test; i++ ) printf( "%d\n", test[i] );`. – Andreas Wenzel Apr 01 '23 at 03:25
  • @AndreasWenzel the result is '110 105 -61 -79 111 32 -61 -95 32 -61 -87 32 -61 -83 32 -61 -77 32 -61 -70 0' – Emilio Hernández Apr 01 '23 at 03:36
  • Could you please change `printf( "%d\n", test[i] );` to `printf( "%02X\n", (unsigned char)test[i] );` That way, it will be hexadecimal and positive, which will probably be more useful. – Andreas Wenzel Apr 01 '23 at 03:39
  • @AndreasWenzel yep that's give me positives numbers ' 110 105 195 177 111 32 195 161 32 195 169 32 195 173 32 195 179 32 195 186 0 ' – Emilio Hernández Apr 01 '23 at 03:42
  • @EmilioHernández: Meanwhile, I have edited my comment to make it hexadecimal, but it doesn't matter. That is already very interesting information. It confirms that your program is indeed sending UTF-8 to your terminal/console. This means that the problem is not your program, but the way your terminal/console is interpreting the data. – Andreas Wenzel Apr 01 '23 at 03:48
  • @AndreasWenzel so... there's a way to change it?? – Emilio Hernández Apr 01 '23 at 03:50
  • I suggest that you try using the function call `SetConsoleOutputCP(CP_UTF8);` as described in the question to which I linked above. Note that you will have to `#include ` to use that function. – Andreas Wenzel Apr 01 '23 at 03:50
  • I believe all you must do is to put the line `SetConsoleOutputCP(CP_UTF8);` immediately above the line `printf("niño á é í ó ú");` and add `#include ` to your program. – Andreas Wenzel Apr 01 '23 at 04:03
  • @AndreasWenzel yep that's work but if is the only ways to resolved that I take it, thank you for you time, but i guess i'll change ide cause is annoying – Emilio Hernández Apr 01 '23 at 04:04
  • 1
    @EmilioHernández: I don't think it is a problem of your IDE, but rather of the Windows Console. Other IDEs will likely have the same problem. – Andreas Wenzel Apr 01 '23 at 04:05
  • @AndreasWenzel it's weird VsCode works good and other like Sublime text 4, this is my first time typing a question how i can thank for answer me? – Emilio Hernández Apr 01 '23 at 04:10
  • @EmilioHernández: Normally, you can accept the person's answer. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) However, in this case, I did not write a formal answer. I only wrote comments, which you cannot accept. So there is nothing you can do, except maybe to post an answer to your own question. [Can I answer my own question?](https://stackoverflow.com/help/self-answer) However, in this case, I believe it would be appropriate to close the question as a duplicate to the other question to which I linked. – Andreas Wenzel Apr 01 '23 at 04:15
  • @AndreasWenzel yep thank you so so much I'm really grateful for your help <3 – Emilio Hernández Apr 01 '23 at 04:24
  • 1
    I am not familiar with VSCode or Sublime text, but it is possible that these IDEs automatically set the Console into UTF-8 mode. You may want to take a look at the settings of those IDEs, and maybe also take a look whether CLion offers any such settings. – Andreas Wenzel Apr 01 '23 at 04:30

0 Answers0