Namaste India
This obfuscated piece of C code prints the map of India to the standard output.
Who wrote this code?
The author is unknown. I have found the code on Quora while searching the web for interesting code snippets. (If you know the author, please let me know so I can give them credit!)
Let's run it
The code compiles and runs correctly, and sure enough it does what it should - outputs a nice ASCII map of India to the standard output.
!!!!!!
!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!
!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!! !!!!!
!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! !!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !
!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!!!!!
!!!!!!!!
!!!!!!
!!!!
How does it work?
The code itself is simple and consists of two for
loops, one inside the other. The outer loop at line 4 walks through the long string of seemingly random letters, while the inner loop at line 11 prints characters to the standard output.
The outer loop
First the variables b
and c
are initialized to 10
. Then, variable a
is used to iterate over the long string. Not the whole string is processed though -- the odd-looking expression used as the index, b+++21
, is understood by the C compiler as b++ + 21
, and skips the first 31
characters altogether. This means the code will jump over the "Hello Folks" message and start working on the string from the capital T
at the beginning of line 5. Since strings in C are null-terminated, the loop will end when it reaches the end of the string. The loop will also increment b
by one for each iteration.
The inner loop
for
;
If the value of a
is lower than 64, this code will do nothing. Otherwise, it will print exactly a-64
characters to the screen. The first character processed by the outer loop is T
, which has the ASCII code of 84
, so it will cause the loop to print 20
characters. Let's now have a look at the expression passed to putchar
, which uses a nice combination of the ternary operator, unusual spacing and operator precedence to confuse the reader.
++c==90 // 'Z'
? c = c / 9
: 33 ^
The code starts with incrementing c
(using the pre-increment operator). If c
equals the ASCII code of capital Z
(90
), it is divided 9
, giving 10
. Since c
is initialized to 10
at the beginning of the outer loop, it will reach 90
after executing the putchar
function 80
times. In this case, putchar
will print a character of the ASCII code 10
, which is a newline, nicely splitting the output into 80-character lines for larger terminals.
If c
has not reached 90
yet (and there is no need to print the newline), the code examines the least significant bit of b
using the expression (b & 1)
. If the bit is set, the value of b
is odd; if it's cleared, b
is even. Knowing that b
is incremented once for every outer loop iteration, we can easily see that 33
xor (b & 1)
will alternate between 32
(for odd b
's) and 33
(for even b
's). Conveniently, these numbers are ASCII codes for space and exclamation mark (!
), which is what is used to print the map of India.
To sum it all up: The code prints spaces and exclamation marks, alternating. The consecutive letters in the string contain the number of the characters to print.
Exercise for the reader
What needs to be done to modify the code to display the map of your country instead?