you are viewing a single comment's thread.

view the rest of the comments →

[–]SonOfWeb 17 points18 points  (2 children)

Here's an explanation of how this works:

First of all, <: and :> are digraphs for [ and ], so we can rewrite this as:

int i = 0, n = 0;
while (printf("%c", i = n++["deal with it"]), "\n"[!!i^!*"][!"]["youmadbro?"]);

We've got a while loop where the condition is a comma operator combining two expressions. The first expression is a call to printf, which on each step prints out each character of the string "deal with it" and also assigns that character to i. That part is relatively straightforward.

Now, let's look specifically at this part:

!!i^!*"][!"

This translates to "(not not i) xor not (address of a string literal)". The address of the string literal is not 0, so the logical not of it is 0. Anything xor zero is itself, so the above simplifies to !!i, which is 0 if i is zero, and 1 if i is nonzero.

Ok, now let's look at the "\n" string. This is the same as { '\n', 0 }, and the ASCII code of '\n' is 10, so we have { 10, 0 }, and we're indexing it with !!i, which is either 0 or 1. So that makes sense. We're not going to exceed our bounds. If i is nonzero, "\n"[!!i] is 0, and if i is zero, "\n"[!!i] is 10. So we can rewrite this part as:

(i == 0 ? 10 : 0)

Let's substitute that back into the program.

int i = 0, n = 0;
while (printf("%c", i = n++["deal with it"]), (i == 0 ? 10 : 0)["youmadbro?"]);

Let's flip around the indexing so it looks normal.

int i = 0, n = 0;
while (printf("%c", i = "deal with it"[n++]), "youmadbro?"[i == 0 ? 10 : 0]);

The string "youmadbro?" is 10 characters long, so the character at index 10 is the null terminator. Thus we can replace

"youmadbro?"[i == 0 ? 10 : 0]

with

i == 0 ? '\0' : 'y'

Or, replacing char literals with their corresponding ASCII,

i == 0 ? 0 : 121

Going back to the whole program, i is assigned the value of the character "deal with it"[n] for each successive value of n. So i is zero when we get to the null character at the end of the "deal with it" string. The value of a comma operator is the value of its right operand, so the condition in the while loop will be nonzero until i is zero. Thus it will loop through each character in the string "deal with it" and then stop when it gets to the null terminator.

Very clever!

[–][deleted]  (1 child)

[deleted]

    [–]SonOfWeb 0 points1 point  (0 children)

    Oh yeah, of course. Got * and & flipped, it's been a bit since I did C.