Yishu Fang
Yishu Fang

Reputation: 9968

How to display the character `£` using c?

After referring the ISO 8859-1 standard, I get to know that the character £ has the value 0xa3, I want to display it using c, so I write this program:

#include <stdio.h>

int main()
{
        printf( "\\xa3 is: %c.\n", '\xa3' );
        printf( "£ is: %c.\n", '£' );

        return 0;
}

I save this source code file in iso 8859-1 encoding. And I hope my program to display £ two times when printf meets each %c.

But, it does not work, why? How can I modify my program to achieve my goal.

I am using linux.

EDIT:

It's display is like this:

\xa3 is: �.
£ is: �.

Upvotes: 0

Views: 488

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754100

It looks as though your terminal is running in UTF-8 rather than 8859-1. Try this:

#include <stdio.h>

int main(void)
{
    printf("\\xC2\\xA3 is: %s.\n", "\xC2\xA3");
    printf("£ is: %s.\n", "£");
    return 0;
}

On my Mac (where the terminal runs in UTF-8), the output is:

\xC2\xA3 is: £.
£ is: £.

Upvotes: 2

unwind
unwind

Reputation: 399881

You need to make sure that the output terminal matches the encoding you've (randomly) chosen for this character. If you're using ISO 8859-1, but your terminal is expecting e.g. UTF-8, you will probably get a result such as the one you're seeing.

Upvotes: 0

Related Questions