GreenElephant
GreenElephant

Reputation: 105

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.

Upvotes: 0

Views: 558

Answers (2)

GreenElephant
GreenElephant

Reputation: 105

I found another way to solve this problem. Maybe someone will find it useful. However, it works only for Windows. For this you need to include the header file <windows.h> and write before entering the following:

SetConsoleCP(1251);
SetConsoleOutputCP(1251);

Then the input/output will be in Windows-1251 encoding.

Upvotes: 0

Adi Efrat
Adi Efrat

Reputation: 16

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

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

Upvotes: 0

Related Questions