0

I have trouble printing some special characters to the terminal using C. But I can do it with python. I think it has something to do with the encoding, like UTF-8.

ref: https://www.codetable.net/hex/c9

#include<stdio.h>

const char *a = "\xc9";

int main(){
    printf("%s\n", a);
}

output:

PS C:\Users\xxx> gcc a.c -o a.exe
PS C:\Users\xxx> .\a.exe
?

a = 0xc9
print(chr(a))

output:

PS C:\Users\xxx> python a.py
É
吴慈霆
  • 523
  • 2
  • 15
  • Printing a single byte `\xC9` is only half of a UTF-8 character; there must be a second byte with a value in the range `\x80` .. `\xBF`, the continuation byte. – Jonathan Leffler Apr 20 '23 at 03:42
  • É (LATIN CAPITAL LETTER E WITH ACUTE) is `\xC9` in the ISO 8859-1 code set, and is also Unicode code point U+00C9. You'd have to generate bytes `\xC3` `\x89` for the UTF-8 representation of that character. Your Python script is working with a different code set from your C code. – Jonathan Leffler Apr 20 '23 at 03:46
  • Does this answer your question? [Unicode, UTF, ASCII, ANSI format differences](https://stackoverflow.com/questions/700187/unicode-utf-ascii-ansi-format-differences) – tripleee Apr 20 '23 at 04:17
  • Note: `\xC9` is `É` only on few systems. And Microsoft used different characters in earlier systems (and depending if you were in US, in Europe, etc.). Now finally Microsoft is changing to UTF-8. So try to write all programs as UTF-8, and apply workaround where needed (but because many programs are UTF-8, Microsoft provide such workaround). The other way is difficult, and it depends on which windows API, culture, etc. your program and your system is using. Try to avoid such (now near obsolete) confusions. – Giacomo Catenazzi Apr 20 '23 at 07:38
  • What if you use `const char *a = "\u00c9";`? – JosefZ Apr 20 '23 at 13:16

0 Answers0