Thread: Printing Russian Characters

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    5

    Printing Russian Characters

    First of all, I'm very sorry if there is a thread which completely solves my problem. I did try to use the search utility but I may not have done so properly.

    What I am trying to accomplish in C is simple. At least, it sounds simple. All I want to do is output Russian characters. By now I've spent 5 or 6 hours trying to figure out how to do this but so far I've been unsuccessful. I've read the Unicode tutorial several times but, as I'm a bit of a novice, I don't quite understand everything which is being said. Also, it seems to me that that tutorial is focused on updating code to make it Unicode friendly, instead of creating something new which is specifically geared towards manipulating Unicode characters.

    I guess what I'll do is post the pathetic bit of code I have written and explain how I arrived at the individual lines.

    Code:
    #include <stdio.h>
    #include <locale.h>
    #include <wchar.h>
    
    
    int main()
    {
        wchar_t dest;
        setlocale(LC_ALL, "ru-RU");
    
    
        mbrtowc(&dest, "Б", 4, NULL);
        wprintf(L"%Lc\n", dest);
    
    
        wprintf(L"Б\n");
    
    
        printf("Б\n");
    }
    1. wchar_t dest;
    In the Unicode tutorial, it's recommended not to use this type of variable but I decided to try it anyway since I couldn't get anything else to work.
    2. setlocale(LC_ALL, "ru-RU");
    From what I understand, I need to include this line because the environment which supports the execution of my code may not otherwise be expecting Russian characters. (Fair enough.) The locale name I'm using may be incorrect. (Does anyone know?)
    3. mbrtowc(&dest, "Б", 4, NULL); wprintf(L"%Lc\n", dest);
    This is my first attempt at outputting a character. In the first line, I'm trying to convert the symbol into a wide character. As previously mentioned, I am a bit of a novice so I'm not entirely sure whether or not I've filled in the fields properly. I used 4 for the size because I read that UTF-8 can require up to 4 bytes.
    4. wprintf(L"Б\n");
    My second attempt, without the conversion.
    5. printf("Б\n");
    My third attempt. I really don't expect this one to work but I've included it anyway.

    Hopefully someone can tell me what it is I'm doing wrong and steer me in the right direction. I'd really like to learn how to properly use Unicode in my programming but I fear I've already spent too many hours in the pursuit...

    By the way, I'm using Windows 7.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I don't have any experience with setlocale() or wchar, but I think it has to do with the font that's loaded - that's used to display the characters.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Quote Originally Posted by nonoob View Post
    I don't have any experience with setlocale() or wchar, but I think it has to do with the font that's loaded - that's used to display the characters.
    That was one of the issues I read about when I was searching for a solution, but I think I've eliminated that particular problem by changing the font in the console from "Raster Fonts" to "Lucida Console". However, I could be wrong...

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Try to run your program in the following way:

    myprogram > myoutput.txt

    And then open myoutput.txt in a text editor. Does it display properly? If yes, then the problem is related to the terminal, not your program.

    Your program outputs the correct Russian character for me, by the way, if I run it with mintty with the character set changed to UTF-8. You can install this program by installing Cygwin and choosing mintty in the installer. On the other hand, Windows's cmd.exe and the associated utilities do not seem to output utf-8. Or at least I don't know how to make them do this.

    So, if you want predictable character handling in a terminal it seems best to use something other than cmd.exe. Alternatives are Cygwin (as I mentioned) and PowerShell for Windows. In Cygwin, for example, you can change the value of environment variables LC_ALL and LANG to cause the standard utilities like ls, cp, etc. to output in utf-8.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Non-English characters with cout
    That covers some of the things you should know.

    >> I don't quite understand everything which is being said.
    What tutorial? Feel free to ask for clarification on anything here.

    gg

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Quote Originally Posted by c99tutorial View Post
    Try to run your program in the following way:

    myprogram > myoutput.txt

    And then open myoutput.txt in a text editor. Does it display properly? If yes, then the problem is related to the terminal, not your program.
    Thanks for the suggestion. Your reasoning makes sense. Unfortunately I can only print the characters to a file in the most primitive way. In other words, it only works if I use the line
    fprintf(filePointer, "Б\n");
    If, however, I try to maintain an array of characters (which I will eventually want to do), it doesn't work. Here's my code:

    Code:
        FILE *filePointer;
        wchar_t alpha[1];
    
    
        filePointer = fopen("test.txt", "w");
        setlocale(LC_ALL, "ru-RU");
    
    
        mbrtowc(&alpha[0], "Б", 4, NULL);
        fprintf(filePointer, L"%Lc\n", alpha[0]);
    
    
        fclose(filePointer);

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Quote Originally Posted by Codeplug View Post
    Non-English characters with cout
    That covers some of the things you should know.
    I could be wrong (and I probably am) but isn't cout an object in C++? If so, I don't think I can use it in a C program.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> but isn't cout an object in C++? If so, I don't think I can use it in a C program.
    Correct. That's just the name of the thread I linked to. You'll just want to read post #22.

    gg

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Quote Originally Posted by Codeplug View Post
    >> but isn't cout an object in C++? If so, I don't think I can use it in a C program.
    Correct. That's just the name of the thread I linked to. You'll just want to read post #22.
    Thanks for pointing that out to me. Unfortunately, I don't think I can make use of the methods you describe in that post. For instance, I don't think the universal character set can be applied to my problem in the same way. At least, I couldn't get it to work.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    On Windows, the easiest solution is to use the MS-CRT from Visual Studio 2008 or later - which allows direct Unicode output:
    Code:
    #include <fcntl.h>
    #include <io.h>
    #include <stdio.h>
    #include <wchar.h>
    
    int main()
    {
        _setmode(_fileno(stdout), _O_U16TEXT);
        
        fputws(L"\u0411\n", stdout);
        
        fputws(L"Б\n", stdout); // save source file as Unicode with BOM
        
        return 0;
    }//main
    gg

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Did you try
    cmd /U
    ?
    You can enter
    help cmd
    in command line for getting help.

  12. #12
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by Shurik View Post
    Did you try
    cmd /U
    ?
    You can enter
    help cmd
    in command line for getting help.
    how to get that command line in cmd?
    will it useful to c proggraming language?

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> will it useful to c proggraming language?
    Nope.

    Quote Originally Posted by cmd /?
    /U Causes the output of internal commands to a pipe or file to be Unicode
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out a matrix of characters
    By Y2R in forum C Programming
    Replies: 7
    Last Post: 04-14-2012, 11:23 PM
  2. Printing Unicode characters
    By stdq in forum C Programming
    Replies: 3
    Last Post: 01-16-2012, 10:43 AM
  3. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  4. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  5. Printing with Unicode characters
    By chibisetsuna7 in forum C Programming
    Replies: 15
    Last Post: 06-19-2006, 10:26 PM