I have read about UTF-8 and Unicode, in what do these characters mean?
However, I have a code in C that when I do:
printf(" \u0305");
I get as a result =̅
because? and also important how can I fix it?. Thank you
I have read about UTF-8 and Unicode, in what do these characters mean?
However, I have a code in C that when I do:
printf(" \u0305");
I get as a result =̅
because? and also important how can I fix it?. Thank you
First, you don't need to use printf. The escape sequence
\u
is interpreted by the C compiler, (not by the standard library, when callingprintf
), just like the sequence\n
for example. It would be the same if you printputs(" \u0305")
.Now, when we write
\u0305
we are telling the C compiler: allocate in this character string a byte for the leading space and then the Unicode codepoint number 0x 0305 (hexadecimal). How this is stored in the string (at compile time, when converting from source code to executable or binary object), depends on the settings (locale
) of your system. If you are using UTF-8, it will be -in this case- two bytes.At print time (either to the console or to a file) those bytes will be output as-is. In order for your eyes to see it as the character that Unicode claims it to be, the console or file viewer must agree with the compiler.
In total, several conditions must be met:
\u
(it happens with almost all of them)What I guess is happening in your case is:
Your compiler is using UTF-8 . The character number 0305 is encoded as the sequence of bytes
0xCC
0x85
Your console is not using UTF.8, but an extended ASCII mode (one byte per character) - and a rather old-fashioned one, CP437 , the one used in DOS . If you look in the table for the (two!) characters 0xCC and 0x85 you will see that they correspond to what you are seeing.
So, what you should do is set your console/terminal so that, when interpreting bytes as text (something that every console does, and it is important that we become aware of that from time to time) it uses UTF-8.
More generally, a programmer should not be dependent on the console. To be sure what your program is printing, redirect the output to a file and view the result with a hex viewer.