Seung
Seung

Reputation: 29

How to print spades, hearts, diamonds, etc. in C and Linux?

So i started off by placing an array of char char symbols[52] = { '\x03' , '\x04', etc. . etc.) and the first time I did it, it actually did print the hearts,spades, etc. but after changing my system locale to a korean one (idk if this is what caused it), it started giving me weird symbols that had nothing to do with it. I also tried to do it in another computer and it actually compiled the symbols correctly. However, then I tried moving it on to linux and it printed weird squares with a 0 0 0 3 in it.

Does anyone know why these appear or is there a better way to print these symbols?

P.S.: I'm using Visual Studio in Windows and then used the .c code in Linux

Upvotes: 1

Views: 13935

Answers (4)

dan04
dan04

Reputation: 91189

Linux systems typically use UTF-8 encoding, in which:

  • ♠ = U+2660 = "\xE2\x99\xA0"
  • ♣ = U+2663 = "\xE2\x99\xA3"
  • ♥ = U+2665 = "\xE2\x99\xA5"
  • ♦ = U+2666 = "\xE2\x99\xA6"

Edit: Unfortunately, the Windows command prompt doesn't support UTF-8, but uses the old DOS code pages. If you want your program to work cross-platform, you'll have to do something like:

#if defined(_WIN32) || defined(__MSDOS__)
   #define SPADE   "\x06"
   #define CLUB    "\x05"
   #define HEART   "\x03"
   #define DIAMOND "\x04"
#else
   #define SPADE   "\xE2\x99\xA0"
   #define CLUB    "\xE2\x99\xA3"
   #define HEART   "\xE2\x99\xA5"
   #define DIAMOND "\xE2\x99\xA6"
#endif

Upvotes: 5

n. m. could be an AI
n. m. could be an AI

Reputation: 120031

On Linux:

  1. Verify that your locale is set to <somelanguage>_<SOMEPLACE>.UTF-8 (type locale in the termunal).
  2. Find a Unicode chart on the web. Verify that you can copy interesting characters with a mouse and paste them to the terninal.
  3. Learn what UTF-8 is.
  4. Write a program.
  5. When it doesn't work, search this site using sonething like "how to print utf-8 in Linux". There are tons and tons of relevant answers. You want to call setlocale(LC_ALL, "") at the beginning of your program, as the language standard requires, and use char, not wchar_t.

On Windows:

  1. Give up and cry. No, really.
  2. Type chcp 65001 in the console. No, really!
  3. Set console fonts to Lucida. No, really!!!
  4. Copy your Linux program over and compile and run it. (You do want to use the same program source on both OSes, right?) When it doesn't work, search for solutions online.
  5. Go to 1.

Upvotes: 0

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13196

If your Linux terminal character set is set to UTF-8, as it usually is, then you should be able to display all of the Unicode glyphs you have fonts for. Suit symbols are in the neighborhood of \u2660.

printf("A\u2660");

Should print "A" and the spade symbol, for example.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263497

Characters like '\x03 are control characters. \x03 in particular is the character you get when you type Control-C (though typing that character often has other effects).

Some fonts happen to assign images to control character. As far as I know, there's no standard for doing so. Apparently in your previous configuration, some of the control character happened to be displayed as playing card suit symbols.

There are standard Unicode characters for those symbols:

2660  BLACK SPADE SUIT
2661  WHITE HEART SUIT
2662  WHITE DIAMOND SUIT
2663  BLACK CLUB SUIT
2664  WHITE SPADE SUIT
2665  BLACK HEART SUIT
2666  BLACK DIAMOND SUIT
2667  WHITE CLUB SUIT

(The numbers are in hexadecimal.)

Different systems encode Unicode in different ways. Windows usually uses some form of UTF-16. Linux usually uses UTF-8, but you can vary that by setting the locale.

Upvotes: 1

Related Questions