1

In my university I have a coursework to write a C project that supports cyrillic input and text on the console and writing to files. But they only allow to use char data type and printf scanf functions.

For the past two days I've been researching how to do that, and found solutions that if you set the encoding to UTF16 and use wchar_t only then cyrillic characters are support. But that makes me use wchar_t functions wprintf, wscanf etc. Not really an option.

Here's the code I have right now but it prints weird symbols on the console.

int main(void) {

setlocale(LC_ALL, "bg-BG");

char str[80];

printf("Въведи текст: "); //Enter text:
scanf("%s", str);

printf("Ти въведе: %s", str); //You entered: 

getch();

return 0;

}

Is there something I'm missing or misunderstanding? As far as I know C/C++ ASCII table supports only english characters with range 0 - 127.

Kyojin
  • 157
  • 2
  • 12
  • There are 8-bit encodings like win1251, KOI-8, etc, having the Cyrillic charset at the codes > 127. But it would really depend on the charset supported by the console. – Eugene Sh. May 24 '17 at 17:20
  • To me, the obvious solution seems to be to set the encoding to UTF-8. You should then be able to read and write Unicode text via the standard `char`-based functions, but be aware that that all non-ASCII Unicode characters will then be represented as two or more consecutive `char` objects. If you're not careful, that can trip you up in various ways. – John Bollinger May 24 '17 at 17:24
  • Why are the wchar_t functions "not really an option"? – Toby May 24 '17 at 17:39
  • So it's possible. But how do I set it to UTF8? The only way I know is using this: _setmode(_fileno(stdout), _O_U8TEXT); but that makes me use wchar_t @Toby it's not an option because my university teacher said that must use only the functions that we used in which case are char based ones – Kyojin May 24 '17 at 17:47
  • Update: tried with `setlocale(LC_ALL, "UTF-8");` and it works, but now the console output that I write with printf() is getting weird symbols. The input is ok.. – Kyojin May 24 '17 at 18:18
  • Are you running this on Windows or Linux? Which console are you using? – cup May 24 '17 at 18:20
  • Windows 7, using the command prompt set with Lucida Console. – Kyojin May 24 '17 at 18:37

1 Answers1

2

Apparantly somehow with luck I managed to make it work using these two:

system("chcp 1251"); setlocale(LC_ALL, "UTF8"); So this is the final solution:

int main(void) {

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

    char str[80];

    printf("Въведи текст: ");
    scanf("%s", str);

    printf("Ти въведе: %s", str);

    getch();

    return 0;
}

Works like charm now

Kyojin
  • 157
  • 2
  • 12