C++ is fun

We all know C++ is a powerful yet complex programming language. But it can also be fun. For instance, you can use emojis and other Unicode characters in the source code. Of course, nobody sane would use emojis for identifiers. But it’s possible and you can have a little fun if have some spare time.

Identifiers are sequences of digits, underscores, lowercase and upper case Latin letters, and most Unicode characters. Identifiers are not allowed to begin with a digit, they must begin with an underscore, a Latin letter, or a non-digit Unicode character. For more information about the Unicode characters in identifiers see this.

So here is a little example of a C++ program that uses emojis. Can you figure out what it does?

This is a screenshot of the program from Visual Studio:

Can we do better (or maybe worse)? Of course, we do. We can do it the Egyptian style writing C++ in hieroglyphs.

In case that’s too small to read perhaps this screenshot is a bit better for reading:

That’s not so far off from this:

(Source)

If you want to check more about the Unicode block of Egyptian hieroglyphs see Unicode Block “Egyptian Hieroglyphs” or Egyptian Hieroglyph.

This program prints 3 to the console, altough that is harder to figure out because I cheated and didn’t show the whole program. This part actually:

The equivalent readable code of the above example is as follows:

Visual Studio seems to have problems displaying some Unicode characters, although I didn’t figure out the pattern. Sometimes they are OK, sometimes they are not. However, the source code is correct and you can successfully build and run a program written in Unicode.

You do have to save the source code using an encoding that allows you to enter the Unicode characters that you want. To do that you have to go to File > Save as … > Save with Encoding and make the appropriate selection.

If you are using Windows 10, there is an emoji application that you can use to type for them. Just type Win + . or Win + ;.

For more information on this see How to type emoji on your PC using Windows 10 Fall Creators Update or Windows 10 Tip: Get started with the emoji keyboard shortcut.

But how do you display Unicode characters in the Windows console? You need to use a code page that supports them. Here is an example:

The output of this is as follows:

However, the Windows console does not currently support emojis (and, in general, Unicode characters from the Supplementary Multilingual Plane. That support will come in the Windows Terminal, which is currently in preview. You can read more about that here. Excited?


via GIPHY

Of course, C++ is not the only programming language that allows emojis in identifiers. You can see examples from other languages here.

2 Replies to “C++ is fun”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.