The following shows ♠♣♥♦ on windows xp, will it for all systems??
#include <stdio.开发者_JS百科h>
int main(int argc, char *argv[])
{
for (int i = 3; i <= 6; ++i)
printf("%c", (char)i);
getchar();
return 0;
}
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.
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.
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?"
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.
精彩评论