So I want to print the copyright symbol and putchar() just cuts off the the most significant byte of the character which results in an unprintable character.
I am using Ubuntu MATE and the encoding I am using is en_US.UTF-8. Now what I know is that the hex value for © is 0xc2a9 and when I try putchar(‘©’ – 0x70) it gives me 9 which has the hex value of 0x39 add 0x70 to it and you’ll get 0xa9 which is the least significant byte of 0xc2a9
JavaScript
x
#include <stdio.h>
main()
{
printf("©n");
putchar('©');
putchar('n');
}
I expect the output to be:
JavaScript
©
©
rather than:
JavaScript
©
�
Advertisement
Answer
The putchar
function takes an int
argument and casts it to an unsigned char
to print it. So you can’t pass it a multibyte character.
You need to call putchar
twice, once for each byte in the codepoint.
JavaScript
putchar(0xc2);
putchar(0xa9);