views:

551

answers:

3

This looks like code in the C language, but I am not completely sure ...

# define v putchar
#   define print(x) main(){v(4+v(v(52)-4));return 0;}/*
#>+++++++4+[>++++++<-]>++++.----.++++.*/
print(202*2);exit();
#define/*>.@*/exit()
+1  A: 

Looks like it might even be a polyglot in C and Brainfuck and perhaps others?

MatrixFrog
+18  A: 

it is a little polyglot for Befunge-93, Brainf*ck, Python, Ruby, Perl, and C that simply prints 404 to stdout.

http://meta.stackoverflow.com/questions/27112/amusing-404-page-not-found-images-for-trilogy-sites

Aaron Harun
+1 for the use of polyglot... word of the day for me :)
Eric U.
+7  A: 

As the original author of the polyglot, and the author of the accepted answer in the meta post, I feel I have the right -- nay, the duty -- to re-post the explanation here:


The easy versions are Python, Perl, and Ruby: the only code executed is

print(202*2);exit();

because they all treat # as a line-comment. Obviously, the code prints "404" and exits the program.


The C code is fairly easy to read, but even easier if you run it through a preprocessor:

main(){putchar(4+putchar(putchar(52)-4));return 0;};exit();

Your standard main function is declared there, and exit is also declared as a function with an implicit return type of int (exit is effectively ignored).

putchar was used because you don't need any #include to use it; you give it an integer argument and it puts the corresponding ASCII character to stdout and returns the same value you gave it. So, we put 52 (which is 4); then we subtract 4 and output 0; then we add 4 to output 4 again.


The brainf*ck code will be a little more difficult to understand, but essentially it's the same as the C code.

+->       Effectively ignored from earlier part of code
++++++++  Put 8 in first memory location
[>++++++<-] Add 6 to second location; decrement first location; 
            repeat until first is 0; effectively this does 6*8 into 2nd location
>++++.    Move back into 2nd location and add 4 so we have a char of 52; print it
----.     Decrement 4 times to output a 0
++++.     Increment 4 times and output a 4
>.        Move pointer and output a null

Actually, that last line wasn't supposed to work that way. The last part was supposed to be ++++< before the >. Oh well, it's up there now.


Befunge is my favorite of the group. I recommend The Visual Befunge Applet if you want to see it in action.

Essentially, all the characters in define are pushed on the stack and never used. Then v points our instruction vector downwards. Then we push another e on the stack, which happens to be an ASCII value of 101. Push 4 on the stack, multiply, turn right, hit the . and print 404 to the screen. @ stops the program there.

Mark Rushakoff
Great job designing it. I'll have a small present for you in 23 hours. ;)
Aaron Harun