I'm using Microsoft Windows 10 with mingw-w64 (gcc version 8.1.0, x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) with cmd
. When I try to print or store and then print a Spanish character on the Windows console, it shows an error. For example I tried to execute this program:
#include <stdio.h>
int main(void) {
char c[20];
printf("pía\n");
scanf("%s", c);
printf("%s", c);
}
If I introduce some Spanish characters the returned sentence is OK but the printed one at the beginning shows an error:
pía
laíóñaú
laíóñaú
Some solutions suggest putting setlocale()
function but the results are the same. Other solution is put the UTF-8 unicode compatibility on region settings:
But now the error is the opposite, the printed one is OK but when I introduce a strange character the console doesn't show it:
pía
lía
l
It is a bit frustrating since all the solutions I have seen are solved with the above or by setting setlocale()
, but none of them work for me and I don't know why.
EDIT
As Mofi say in comments I try to use SetConsoleCP()
and SetConsoleOutputCP()
to change the code page of the console. Without fully understanding how these functions work, with the same code as above, I ran several examples with wrong results:
pía | p├¡a | p├¡a | pía
lía | lía | lía | lía
l | l | lía | la
input: 65001 output 65001 | input: 65001 output 850 | input: 850 output 850 | input: 850 output 65001
How I don't fully understand this functions I don't know why in the last example, the console don't show the accented stored character but in the printed one it does and in the example above the opposite happens.