ccmdencodingconsolewindows-1251

Incorrect output to the console in C


The purpose of the program is to get a symbol and then immediately display it on the screen. But the problem is that it outputs a different character, or nothing at all. I found that the output is in Windows-1251 encoding, and the input is in CP866. How do I solve this problem? How to make both output and input in Windows-1251 encoding.

Post Scriptum: the problem appears when you enter a Cyrillic character.

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

int main(void)
{
    setlocale(LC_ALL, "");

    printf("Уведіть символ.\n");

    char ch = getchar();

    printf("\n");

    printf("%c", ch);

    getchar();
    getchar();

    return 0;
}

I tried to use wchar_t (respectively procedures wprintf(), getwchar()), but the situation did not change.


Solution

  • Maybe this will help? https://stackoverflow.com/a/44167461/8893124 they suggest to set:

    system("chcp 1251");
    setlocale(LC_ALL, "UTF8");