Skip to content
Advertisement

Why doesn’t putchar() output the copyright symbol while printf() does?

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

#include <stdio.h>

main()
{
    printf("©n");
    putchar('©');
    putchar('n');

}

I expect the output to be:

©
©

rather than:

©
�

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.

putchar(0xc2);
putchar(0xa9);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement