C program to print map of India

Write a C program to print map of India.


Below is obfuscated version in C that generates the map of India. Obfuscated code is any code that is difficult for humans to understand.


Download   Run Code


Map of India


Basically, the string is a run-length encoding of the map of India. Alternating characters in the string stores how many times to draw a space, and how many times to draw an exclamation mark consecutively. Here’s an analysis of the different elements of this program –

The encoded string –

“Greetings from Techie Delight!!TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJ
Aelclcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!”

Notice [b+++21] at the end of the encoded string. As b+++21 is equivalent to (b++ + 21) which will evaluate to 31 (10 + 21), the first 31 characters of this string are ignored and do not contribute to anything. The remaining encoded string contain instructions for drawing the map. The individual characters determine how many spaces or exclamation marks to draw consecutively.

Outer for loop: This loop goes over the characters in the string. Each iteration increases the value of b by one, and assigns the next character in the string to a.

Inner for loop: This loop draws individual characters, and a newline whenever it reaches the end of line. Consider below putchar statement

putchar(++c==’Z’ ? c = c/9 : 33^b&1);

As ‘Z’ represents number 90 in ASCII, 90/9 will give us 10 which is a newline character. Decimal 33 is ASCII for ‘!’. Toggling the low-order bit of 33 gives you 32, which is ASCII for a space. This causes ! to be printed if b is odd, and a blank space to be printed if b is even.

Below is less obfuscated version of above code –


Download   Run Code

Author: Aditya Goel

Reference: http://stackoverflow.com/questions/3533348/how-does-this-code-generate-the-map-of-india

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Sort by:   newest | oldest | most voted
Burak Alkan
Burak Alkan