简体   繁体   中英

How to print characters from Latin 2 code page in C (without string)?

I'm struggling to print "ę" in this sentence in C:

...W Katalogu o nazwie "c:\roboczy" znajduje się 90% dobrze napisanych programów w C...

My cmd's encoding page is CP-852 ("Latin 2"). I'm writting in Dev-C++ 5.11 and my compiler is TDM-GCC 4.9.2 64-bit Release.

I've managed to print the backslash and ó using this script:

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

int main()
{
   char znak1 = 92;

    printf("...W Katalogu o nazwie %\"c:%croboczy%\" znajduje sie 90%% dobrze napisanych program\242w 
    w C...\n",znak1);
    return 0;
}

Of course right now it's just "e" in there, but neither inserting "\281" directly into the printf nor putting 0x119 into a char like znak1 up there has worked.

For example whith "\281" inserted I get:

...W Katalogu o nazwie "c:\roboczy" znajduje si☻81 90% dobrze napisanych programów w C...

While adding a char variable like znak1 with value 0x119 yields:

...W Katalogu o nazwie "c:\roboczy" znajduje si↓ 90% dobrze napisanych programów w C...

I understand I can simply hold on to ASCII characters and the output will be readable but it bugs me not to be able to do such a simple thing in this language. I'll appreciate some tips on what to do!

Cheers: :)

Okay, so it's been pointed out to me that "Latin 2" is not that presice. While searching for codes for characters precisely for CP-852 I found, tried and got results from:

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

int main()
{
    char znak1 = 0xA9;
    char znak2 = 0xA2;
    
    printf("...W Katalogu o nazwie \"c:\\croboczy\" znajduje si%c 90%% dobrze 
    napisanych program%cw w C...\n",znak1,znak2);
    return 0;
}

Sorry for the fuss.

Even now that you already found a way to reach your goal, I'll explain the result of your try.

For example whith "\281" inserted I get:

 ...W Katalogu o nazwie "c:\roboczy" znajduje si☻81 90% dobrze napisanych programów w C...

Regardless of how you arrived at the wrong code, 281 is not a valid octal number, since that has only digits from 0 to 7. So only \2 was recognized as an escape sequence for , and 81 were printed literally.

By the way, instead of inserting the character with printf and %c , you can as well directly write the escape sequence into the string, more easily hexadecimal than octal: "… znajduje si\xA9 …" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM