I’m still playing around with C to understand how it works.
I’m having trouble printing characters from the extended ASCII table (128-255). If I do printf("Â")
(for example) it prints  (everything works fine). However if I assign a variable for instance a = 194
and then print the variable printf("%c",a)
it prints � instead of Â.
By the way it works fine with the 32-127 characters (for example 35 prints #)
-> How could I print one of the 128-255 character from an integer (decimal or binary)? Any help will be appreciated.
I am using gcc11.3 on Ubuntu 20.04.1 LTS
2
Answers
It is likely both your compiler and the terminal use UTF-8 to encode non-ASCII characters. You can verify this with this code:
The output should be
Â: len=2, bytes=C3 82
.To convert non-ASCII characters to UTF-8 sequences on output streams, you can use the locale functions from
<locale.h>
and wide character output:Output:
If the locale is correctly configured in the terminal, you can select the default locale with
setlocale(LC_ALL, "");
As pointed out by @interjay, as well as written on Wikipedia:
🔗 Wikipedia: Extended ASCII
Also, you are able to print  while using
printf("Â");
because you are using it as a string. Â is interpreted as a Unicode character (by both your compiler and your terminal). You can check it via compiling the following:On my system, my compiler gives me this warning:
which suggests that  is indeed a Unicode multi-byte character.
You can also try running this code to check what  expands to:
and, its output:
So, your  is a multi-byte character expanding to those values. If you try printing each of these characters separately, you will again see ��. Thus, when printed together, the terminal would interpret it as a Unicode character and print the desired result.
BTW, I also found this on the internet:
Notice the use of the words Windows-1252 character set. Although I have no idea about that character set, the most probable reason for
194
not printing asÂ
might be due to your terminal not supporting that character set.