views:

402

answers:

4

The following shows ♠♣♥♦ on windows xp, will it for all systems??

#include <stdio.h>
int main(int argc, char *argv[])
{
    for (int i = 3; i <= 6; ++i)
        printf("%c", (char)i);

    getchar();
    return 0;
}
+11  A: 

Nope. Character encoding is a very platform dependent, in my experience.

Consider, in ASCII those characters don't even exist. And I have no clue where they are in Unicode. And where ever they are, you would then be depending on how your platform outputs Unicode.

GMan
This is correct. It does not work on OS X.
Matt B.
What an "ace" answer.
Skurmedel
Yeah - it suits the question very well.
Steve Fallows
@Skurmedel @Steve: **... :|**
GMan
You guys are too much...
Zack Mulgrew
Oh, I heart you.
Skurmedel
Stop punning or I'll club you to death!
Dan Moulding
I suppose a serious discussion just isn't in the cards...
Adam Maras
These comments collapsed like a house of cards.
Skurmedel
bunch of jokers
fireeyedboy
What the deuce is this thread all about?
Brian Campbell
Unicode codepoints are 0x2660 through 0x2667 for the suits. Four are solid black and four are white with black outlines.
Mike DeSimone
@Brian: about deciding who is king or queen of puns, and who know jack about it.
fireeyedboy
I wish you would all dia... mond.
Jason
Puns, what a flop.
GMan
@GMan: Cry me a river
fireeyedboy
Let's get something straight; You're being a royal pain, and it's about time someone called you out on it.
GMan
@fireeyedboy When did the conversation take a turn for the worse?
Brian Campbell
I bet it's because of GMan. I had to take his remarks all in while he just said them with a straight face.
fireeyedboy
Well check it out; fireeyedboy thinks he's gonna blind me with big talk. You better quit your bluffing before you muck everything up.
GMan
NUTS! Sounds like we should put everyone out of their misery and start up a table on PokerStars.net :)
Robert
+3  A: 

Printing out control characters (and that's what the low values are) that aren't commonly used (like '\n') will lead to different results on different platforms.

One approach to being cross-platform would be Unicode (the suits are on the game symbols page, although it's common not to support such characters in fonts.

David Thornley
+11  A: 

No, those are control characters in ASCII. The terminal you are printing them out in used CP437, which was the character set of the original IBM PC. It uses the same numbering for characters that are in ASCII, but extended ASCII with accented letters, greek letters, box drawing characters, and a few dingbats. Needless to say, it is completely non-portable; your program will only work in a Windows command terminal, not on any other system; and those characters won't even work correctly in the more common Windows encoding, CP1252.

If you want to portably print out special characters, you should use Unicode which is a universal character set, in which the card suits are at 0x2660–0x2667. See The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) for a high-level overview of Unicode.

Note that how you enable your application to use Unicode differs between Unix and Windows, and you still need to convert between different encodings as Unicode has more than one encoding (including partial encodings, such as legacy character sets), but at least you can work with the same universal character set no matter what platform you're on or encoding you're using.

Brian Campbell
Quite a few compilers can actually accept `L"♠♣♥♦"`
MSalters
+6  A: 

Unicode and UTF-8 for the suits:

  • ♠ = 0x2660 = 0xE2 0x99 0xA0
  • ♥ = 0x2665 = 0xE2 0x99 0xA5
  • ♦ = 0x2666 = 0xE2 0x99 0xA6
  • ♣ = 0x2663 = 0xE2 0x99 0xA3
  • ♤ = 0x2664 = 0xE2 0x99 0xA4
  • ♡ = 0x2661 = 0xE2 0x99 0xA1
  • ♢ = 0x2662 = 0xE2 0x99 0xA2
  • ♧ = 0x2667 = 0xE2 0x99 0xA7

Mac OS X Character Search Palette FTW.

It might look mixed up code-wise, but there's a method. 0x2660..0x2663 are the standard four, with white instead of red (♠♡♢♣), and 0x2660..0x2663 are the same set inverted in color (♤♥♦♧).

P.S. Not trying to ruin the pun fun, but I figured someone would want an answer to "well, what is the standard way supposed to be?"

Mike DeSimone