Hello I was just wondering how I can display the infinity (∞) in C++? I am using CodeBlocks. I read couple of Q&A's on this topic but I'm a newbie at this stuff, especially with Hex coding and stuff. What do I have to include and what do I type out exactly. If someone can write the code and explain it, that'd be great! Thanks!
-
1Where are you wanting to display it? In a GUI? (If so, what platform?), in a web-application? Mainframe? Command-line terminal/console? Something else? – Dai Apr 03 '15 at 17:21
-
Um, in the basic command prompt looking window? (Is that a legitimate answer?). Nothing fancy, just basic coding with CodeBlocks. – Maty Apr 03 '15 at 17:26
2 Answers
The symbol is not part of the ASCII code. However, in the code page 437 (most of the time the default in Windows Command Prompt with English locales/US regional settings) it is represented as the character #236. So in principle
std::cout << static_cast<unsigned char>(236);
should display it, but the result depends on the current locale/encoding. On my Mac (OS X) it is not displayed properly.
The best way to go about it is to use the UNICODE set of characters (which standardized a large amount of characters/symbols). In this case,
std::cout << "\u221E";
should do the job, as the UNICODE character #221 represents inf
.
However, to be able to display UNICODE, your output device should support UTF encoding. On my Mac, the Terminal uses UTF, however Windows Command Prompt still uses the old ASCII encoding CodePage 437 (thanks to @chris for pointing this out). According to this answer, you can change to UNICODE by typing
chcp 65001
in a Command Prompt.
-
It's in CP437? What luck! Out of curiosity, is [this](http://www.fileformat.info/info/unicode/char/00ec/index.htm) what the Mac shows (`Ì`)? – chris Apr 03 '15 at 17:27
-
-
-
This worked perfect! I don't know how the OS deals with ASCII or any technical information, but it works, that's all I need. Thanks! – Maty Apr 03 '15 at 17:33
-
@Maty It may be a better idea to use the unicode, as mention in the AD's answer, so your code will be portable. – vsoftco Apr 03 '15 at 17:33
-
@Maty, Assuming an ASCII-compatible system, the characters 128 and beyond are dependent on the system. There are many code pages that define different sets of characters to fill those places. The Windows console uses code page 437 by default. When you give it 236, it assumes that you mean the character represented by the number 236 within code page 437. As soon as you move to something that uses a different code page (for example, Windows 1252), it will work differently. Unicode actually standardized which number goes which character, and that is 0x221E to the infinity symbol. – chris Apr 03 '15 at 17:37
-
There is no such thing as "[Extended ASCII](http://en.wikipedia.org/wiki/Extended_ASCII)", [ASCII](http://en.wikipedia.org/wiki/ASCII) is a 7-bit encoding, characters with codes above 127 are codepage-dependant. – n0rd Apr 03 '15 at 17:55
-
@n0rd it's still often called the Extended ASCII table though. I know it's not the best name, not standard, but I have seen it a lot, and I prefer to keep my answer concise, that's why I used it. I updated my answer to point out that the terminology is non-standard. – vsoftco Apr 03 '15 at 17:57
-
Being "often called" does not make it right and you support that wrong terminology and mindset. – n0rd Apr 03 '15 at 18:12
-
@n0rd please see the updated answer. The OP asked a simple question, I wanted to provide a simple answer, without going into details about encodings/locales and so on. I mentioned explicitly in my answer that the result is dependent on the OS encoding. And except that Wikipedia article that is just saying that the term is "sometimes criticized", I cannot find any reliable source saying that "extended ASCII" is a wrong term, and we should stop using it, so I do not believe to be the wrong terminology/mindset. In any case, I think UNICODE is the way to go here. – vsoftco Apr 03 '15 at 18:21
-
You obviously should be looking for reliable source that define it, not the one that says the opposite. "Extended ASCII" is a collective term for "anything that is not ASCII" which is meaningless and misleading as it might create an illusion, that there is a standard for 8-bit characters with codes of 128 and above. – n0rd Apr 03 '15 at 18:46
-
@n0rd OK I see you are truly agains the terminology, and I don't have a strong opinion about it, so I just removed it, hope you're happier :) – vsoftco Apr 03 '15 at 18:56
-
Two more comments: "Basic ASCII code" suggests that non-basic variant also exists. Which is not true. And CP437 is not default encoding for windows console, it depends on regional settings and only true if Windows is set up for English locale. Thank you. – n0rd Apr 03 '15 at 19:00
You can show it through its UNICODE
∞ has the value: \u221E
You can show any character from the Character Map by its unicode.
-
you should mention that `std::cout << "\u221E";` works as desired (confirming this on my platform). – vsoftco Apr 03 '15 at 17:32
-
I saw this somewhere before, tried it. I get 3 weird characters. It doesn't work for me. These are the output characters that I get: Γê₧ – Maty Apr 03 '15 at 17:34
-
1@Maty yeah, probably because of the console details/locales/lack of UTF encoding etc. – vsoftco Apr 03 '15 at 17:35
-
Anything I can do about this? Say for future ASCII characters that I might need. – Maty Apr 03 '15 at 17:37
-
@Maty is better to try to use a console that supports UTF encoding for using UNICODE. Try `chcp 65001` in console to change the encoding to UTF-8, see here http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how/388500#388500 – vsoftco Apr 03 '15 at 17:39