What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 122 Stack Exchange communities.

I have journeyed thus and bruised into the lands of the error page on Stack Overflow. There, upon the hill I see a vision as such:

Stack Overflow 404

For those reading with images off:

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

How can I compile it?

More to the point, what is the joke?

share|improve this question
7  
A little context might be helpful. Where did you see this on SO? –  Bill the Lizard Nov 4 '09 at 23:53
    
LOL, that's hilarious! –  Ether Nov 5 '09 at 0:01
    
Have a look at this collection of 404 pages. –  Lazer Jun 23 '10 at 16:11
1  
What's this font? –  Matt Alexander Jun 23 '10 at 20:49
    
Liberation Mono looks similar. –  Lazer Jun 24 '10 at 3:31
1  
Close, but no cigar. I submitted it to WTF: new.myfonts.com/WhatTheFont/forum/case/295724/?flush=1 –  Matt Alexander Jun 26 '10 at 0:05
1  
Just to follow up, the font is Pragmata: new.myfonts.com/fonts/fsd/pragmata/tt –  Matt Alexander Jul 21 '10 at 21:44
    
I think there's a bug in the Befunge version: # - skip next cell (it's a space anyway) define - pushed to stack (space is ignored) v - turn down e44 - pushed to stack . - pop value and print as integer, output so far is "4" definee4 - current stack * - pop 4, pop e, mult and push define404 - current stack (404 is one value) > - turn right . - pop and print as integer, "4404" is current output @ - end –  Alowishus Drunkwater Feb 27 '13 at 18:30
    
@Alowishus, you (and some earlier editors) have missed the "newline symbol" in the image; meanwhile Beetle has fixed the text version. –  Arjan Apr 21 '13 at 6:47
add comment

7 Answers

up vote 195 down vote accepted

Want a step-by-step through it? I'm the original author of the polyglot.

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.

share|improve this answer
3  
Is there a room for whitespace version? en.wikipedia.org/wiki/Whitespace_(programming_language) –  Juha Syrjälä Nov 10 '09 at 18:56
15  
Yes, there should be room, but since the 404 is an image, it would be somewhat difficult to convey whitespace :) –  Mark Rushakoff Nov 10 '09 at 23:19
7  
Btw, it is executable PHP as well (codepad.org/Med97xh4) (With the exception of the opening PHP tag.) –  xbonez Jun 7 '12 at 1:34
    
@MartijnPieters: Yes, there is an exit pseudo-builtin. –  nneonneo Mar 1 '13 at 21:47
    
@nneonneo: Gah, of course there is. –  Martijn Pieters Mar 1 '13 at 21:48
1  
WRONG: See ISO/IEC 9899-1999 §7.19.7.9-1. putchar is supposed to be defined in <stdio.h>, not implicitly. –  Cole Johnson Apr 25 '13 at 13:56
add comment

Here's my shot at a 404. It's not a polyglot, but it's more visually interesting:

#define _ f++>o--*ur-- || o--*h++ || f++*o--*ur;
int f = 0, o = 0, ur = 0, h = 0;
main(){f++;o--*ur;o--*h;f*our();printf("%d\n", (f-o-ur));}/*oh, f*/our(){
_-_     _-_      _-_      _-_     _-_
_-_     _-_     -_-_-     _-_     _-_
_-_     _-_  _-_-   -_-_  _-_     _-_
_-_-_-_-_-_  _-_-   -_-_  _-_-_-_-_-_
_-_-_-_-_-_  _-_-   -_-_  _-_-_-_-_-_
        _-_  _-_-   -_-_          _-_
        _-_  _-_-   -_-_          _-_
        _-_  _-_-   -_-_          _-_
        _-_     -_-_-             _-_
        _-_      _-_              _-_
}
share|improve this answer
5  
LOL. Very nice. –  Jon Seigel Jun 23 '10 at 23:44
    
How do you even construct such a program? –  usr Mar 25 '12 at 21:39
1  
Heavy use of macros. –  d_r_w Mar 26 '12 at 17:02
1  
-_- (my face when I saw it) –  David Is Not Here Dec 20 '13 at 17:02
add comment

It's from here. It's a Polyglot.

share|improve this answer
6  
It is actually from the official 404 page on SO now. –  jjnguy Nov 5 '09 at 0:15
add comment

The joke is that it is a program that prints '404' to the screen.

The program is a polyglot. It is valid in C, Python, Perl, Ruby, Befunge-93, and Brainf*ck.

share|improve this answer
add comment

For copying purposes:

404.poly

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

And to see the output under the different interpreters:

404.sh

#!/bin/sh
echo 'ruby:      ' $(ruby 404.poly)
echo 'python:    ' $(python 404.poly)
echo 'perl:      ' $(perl 404.poly)
echo 'brainfuck: ' $(bf 404.poly)
echo 'c:         ' $(gcc -xc -o404 404.poly 2>/dev/null && ./404)

#     befunge:     http://www.ashleymills.com/?q=befunge%5Fapplet%5Flite
share|improve this answer
1  
If I made a typo fix it! –  badp Jul 18 '10 at 9:13
add comment

I added in a whitespace 404 output.

# define v putchar               

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

The tabs don't come accross very well:

#_define_v_putchar[tab][tab]__[tab]_[tab]_
_
#define_print(x)[tab]main(){_v(4+v(v(52)-4));return_0;_}/*[tab]
#>+++++++4+[>++++++<-]>++++.----.++++.*/_[tab]
print(202*2);exit();
#define/*>.@/exit()
<blank line>

The first line puts the value 202 on the stack

  • (SS) Command to put a number on that stack
  • (STTSSTSTS) 202 in binary (S=0, T=1)
  • (L) Ends the number

The next 404 is produced

  • (SLS) Duplicate the top of the stack (adds a second 202)
  • (TS) Arithmetic operation
  • (SS) Adds the top two numbers on the stack (202 + 202 = 404)

Finally 404 is printed

  • (TL) IO command
  • (ST) Indicates the top of the stack should be printed as an integer

The last two just end the program

  • (LL) End of program
share|improve this answer
    
Here is an online interpreter for whitespace. –  Cemafor Feb 26 '13 at 17:02
    
Maybe the newlines/some copy/paste artifact is affecting it, but that gives a syntax error for me –  Ben Brocka Feb 26 '13 at 19:30
    
Sorry, I forgot about the space in the "return 0;" and somehow dropped it out when testing. The new version should work. –  Cemafor Feb 27 '13 at 5:17
add comment

It's a polyglot for C (not C++), brainf***, Python, Perl, Ruby, and (nearly) any other scripting language for that matter. Interestingly, it is also a Befunge-93 program. Here's a comprehensive breakdown.


C

After preprocessing, the program becomes:

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

The function putchar is stated by the standard as int putchar(int c) inside <stdio.h>. As there is no #include directive, this is not a valid C program either. It could be valid if there is a compiler that implicitly includes <stdio.h> if it notices certain functions being used, but I have yet to encounter one. It could also be valid if you were using gcc and added -include "{stdio}" to the command line. However, the -include parameter expects a relative path.

If there was an #include <stdio.h> line, the program would still be valid in scripting languages (as explained below), and brainf*** as the only control characters are <>. It would not function however as the pointer is set to 0 on start, and setting it to -1 should crash the interpreter.

Disregarding all of that, when we reformat the code a bit, and replace 52 with its ASCII equivalent ('4'), we get:

int main() {
    putchar(4 + putchar(putchar('4') - 4));
    return 0;
}

As for the putchar declaration, it is defined by the standard to return it's input, like realloc. First, this program prints a 4, then takes the ASCII value (52), subtracts 4 (48), prints that (ASCII 0), adds 4 (52), prints that (4), then finally terminates. This results in the following output:

404

As for this polyglot being valid C++, unfortunately, it is not as C++ requires an explicit return type for functions. This program takes advantage of the fact that C requires functions without an explicit return type to be int.


brainf***

brainf*** works by reading its input character by character, and ignoring anything except the brainf*** operators (any of .<>[]+-). This results in the following (line breaks included, sans first line):

+-
>++++++++[>++++++<-]>++++.----.++++.

>.

Stepping through this program, we get:

+- ; nothing
>  ; set ptr to 1
++++++++ ; set arr[1] to 8 (iter count)
[
  > ; set ptr to 2
  ++++++ ; add 6 to arr[2]
  < ; set ptr to 1
  - ; decrement loop count
] ; arr[2] now contains 48 (6*8)
> ; set ptr to 2
++++. ; set arr[2] to 52 ('4') and print
----. ; set arr[2] to 48 ('0') and print
++++. ; set arr[2] to 52 and print
>. ; print arr[3] (`\0`)

The reasoning behind the output of a null character at the end is unknown to me. However, this all results in the same output as above:

404

Scripting languages

Nearly all popular scripting languages (Perl, Python, Ruby, etc.) contain a function called print that casts whatever is passed to it to a string then writes it to stdout. They also interpret # as a single line comment (akin C and C++'s //).

This results in the following with the "comments" removed:

print(202*2);
exit();

It should be obvious what this does.


Befunge-93

TODO

# define v putchar
#   define print(x) main(){v(4+v(v(52)-4));return 0;}/*
#>+++++++4+[>++++++<-]>++++.----.++++.*/
print(202*2);exit();
#define/*>.@*/exit()
share|improve this answer
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .