-2

I'm trying to save a character from the cyrillic alphabet in a char. When I take a string from the console it saves it in the char array successfully but just initializing it doesn't seem to work. I get "programName.exe has stopped working" when trying to run it.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>

void test(){
    char test = 'Я';
    printf("%s",test);
}

void main(){
    SetConsoleOutputCP(1251);
    SetConsoleCP(1251);
    test();
}


fgets ( books[booksCount].bookTitle, 80, stdin ); // this seems to be working ok with ascii.

I tried using wchar_t but I get the same results.

Binarian
  • 12,296
  • 8
  • 53
  • 84
  • 6
    Now go read a beginner C tutorial about `printf()`, and read its documentation as well. Also, turn on compiler warnings. – The Paramagnetic Croissant May 25 '14 at 13:22
  • Passing "%s" to printf tells it to expect a string parameter, but you are passing a char. – David Dubois May 25 '14 at 13:26
  • anyways printing it with %c is giving me a question mark – Иван Божков May 25 '14 at 13:27
  • 2
    You need to make sure your *input* character `'Я'` is stored in a single byte as well. Chances are it appears encoded as UTF-8 in your source code. Look up the character code for `Я` in code page 1251 and use that instead of the literal character. – Jongware May 25 '14 at 13:41
  • Read http://utf8everwhere.org/ and remember that in 2018 (and even 2014) it is reasonable to use UTF-8 everywhere. Then the `в` - the last letter of your family name - is not a single byte in UTF-8 – Basile Starynkevitch Sep 29 '18 at 05:16

2 Answers2

1

This could be solved, doing

void test()
{
    char test = 'Я';
    putchar(test);
}

But there is a catch: Since 'Я' is not an ASCII character, you might need to set appropriate locale before. Moreover, only ASCII characters 32 - 126 are guaranteed to be printable, and the same symbol, on all systems.

Andro
  • 2,232
  • 1
  • 27
  • 40
  • `only ASCII characters 32 - 126 are guaranteed to be printable, and the same symbol, on all systems` is wrong. Even ASCII isn't mandated by the C standard. For example there are many systems that use EBCDIC instead of ASCII – phuclv Sep 29 '18 at 05:07
1

If you're using Russian Windows which uses Windows-1251 codepage by default, you can print the character encoded as a single byte using the old printf but you need to make sure that the source code uses the same cp1251 charset. Don't save as Unicode.

But the preferred way should be using wprintf with wide char string

void test() {
    wchar_t  test_char   = L'Я';
    wchar_t *test_string = L"АБВГ"; // or LPCWSTR test_string
    wprintf(L"%c\n%s", test_char, test_string);
}

This time you need to save the file as Unicode (UTF-8 or UTF-16)

UTF-8 may be better, but it's trickier on Windows. Moreover if you use UTF-8 you cannot use a char to store Я because it needs more than 1 byte. You must use a char* instead

Note that main must return int, not void, and the above fgets must be called from inside some function

phuclv
  • 37,963
  • 15
  • 156
  • 475