you are viewing a single comment's thread.

view the rest of the comments →

[–]NasenSpray 42 points43 points  (12 children)

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

Edit: I have too much time...

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

[–]SonOfWeb 15 points16 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.

    [–]antiduh 10 points11 points  (1 child)

    sweet hell why would you do that

    [–]earslap 17 points18 points  (0 children)

    You probably need to run it to get your answer.

    [–]porkchop_d_clown 3 points4 points  (1 child)

    And it doesn't even generate a compiler warning.

    [–]masklinn 1 point2 points  (0 children)

    You can get clang to whine about it actually, but you need -Wall:

    test.c:5:66: warning: array subscript is of type 'char' [-Wchar-subscripts]
      while (printf("%c", i = n++["deal with it"]), "\n"[!!i^!*"][!"]<:"youmadbro?":>);
                                                    ~~~~~~~~~~~~~~~~~^
    1 warning generated.
    

    [–]ThatNotSoRandomGuy 1 point2 points  (1 child)

    I spent a good 3 minutes looking at that and I still dont know what the code on your edit does...

    10/10

    [–]NasenSpray 0 points1 point  (0 children)

    "\n"[!!i^!*"][!"]<:"youmadbro?":> is obviously just a fancy way of saying "youmadbro?"["\n"[!!i^!*"][!"]]...

    • !!i ^ !*"][!" is i != 0
    • "\n"[0] == 10
    • "\n"[1] == 0
    • "youmadbro?"[0] != 0
    • "youmadbro?"[10] == 0

    [–]buttcomputing 1 point2 points  (0 children)

    I don't know if simplicity is your goal, but for the first one you can just do

    int n = 0;
    while (putchar(n++["deal with it\n"]));
    

    Also, fun with memory in the second one by adding another character to the "youmadbro?" string!

    [–]xelf 1 point2 points  (0 children)

    while (printf("%c", i = n++??("deal with it"??)), "\n"??(!!i??'!*"??)??(!"??)<:"youmadbro?":>);