515

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?

11
  • 14
    A little context might be helpful. Where did you see this on SO?
    – Bill the Lizard Mod
    Commented Nov 4, 2009 at 23:53
  • 3
    Have a look at this collection of 404 pages.
    – Lazer
    Commented Jun 23, 2010 at 16:11
  • 2
    What's this font?
    – Matt Alexander
    Commented Jun 23, 2010 at 20:49
  • 1
    Liberation Mono looks similar.
    – Lazer
    Commented Jun 24, 2010 at 3:31
  • 2
    Close, but no cigar. I submitted it to WTF: new.myfonts.com/WhatTheFont/forum/case/295724/?flush=1
    – Matt Alexander
    Commented Jun 26, 2010 at 0:05
  • 9
    Just to follow up, the font is Pragmata: new.myfonts.com/fonts/fsd/pragmata/tt
    – Matt Alexander
    Commented Jul 21, 2010 at 21:44
  • 1
    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
    Commented Feb 27, 2013 at 18:30
  • 2
    @Alowishus, you (and some earlier editors) have missed the "newline symbol" in the image; meanwhile Beetle has fixed the text version.
    – Arjan
    Commented Apr 21, 2013 at 6:47
  • 1
    It looks C code.
    – peterh
    Commented Aug 18, 2019 at 22:42
  • 1
    I compiled it on a Linux. The compiler gave many warnings, but the result is very simple: it prints the string "404" to the stdout. The post is clearly on-topic.
    – peterh
    Commented Dec 29, 2019 at 20:14
  • 1
    RIP the error page :( Commented Sep 1, 2021 at 14:21

8 Answers 8

503

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.

10
  • 17
    Is there a room for whitespace version? en.wikipedia.org/wiki/Whitespace_(programming_language)
    – Juha Syrjälä
    Commented Nov 10, 2009 at 18:56
  • 60
    Yes, there should be room, but since the 404 is an image, it would be somewhat difficult to convey whitespace :)
    – Mark Rushakoff
    Commented Nov 10, 2009 at 23:19
  • 18
    Btw, it is executable PHP as well (codepad.org/Med97xh4) (With the exception of the opening PHP tag.)
    – Ayush
    Commented Jun 7, 2012 at 1:34
  • 2
    @MartijnPieters: Yes, there is an exit pseudo-builtin.
    – nneonneo
    Commented Mar 1, 2013 at 21:47
  • 2
    @nneonneo: Gah, of course there is. Commented Mar 1, 2013 at 21:48
  • 4
    WRONG: See ISO/IEC 9899-1999 §7.19.7.9-1. putchar is supposed to be defined in <stdio.h>, not implicitly.
    – Cole Johnson
    Commented Apr 25, 2013 at 13:56
  • 24
    @Cole Johnson - OBJECTION, IRRELEVANT (or pedantic, or missing the point, take your pick): He said "you don't need any #include to use it", he did not say it's a built-in. Whether or not it's implicit doesn't matter. It emits a warning, but the implicit declaration works (tested in llvm, gcc & visual studio).
    – nevelis
    Commented Aug 8, 2014 at 7:52
  • 1
    Ok, now I have an objection (befunge one)... the problem is, in the standard befunge93 instructions, writing just 'e' doesn't mean anything. It's simply ignored. And by befunge98 standard, it means 15 not 101. if you wanted to actually put character 101 in the buffer, you had to escape it.
    – Ali1S232
    Commented Feb 20, 2015 at 20:15
  • 1
    @Cole Johnson: Well, apparently the author was aiming at something around C89/90 where calling undeclared functions was still legal. Albeit plain main() is illegal in C89/90 as well. So, formally this is not standard C but sort of "street C"/"pidgin C". Commented Feb 12, 2018 at 8:39
  • 2
    The Befunge code is not to spec, and the only implementation that it appears to work in (the one you've linked) appears to have been deleted.
    – Jo King
    Commented Mar 1, 2018 at 0:50
105

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(){
_-_     _-_      _-_      _-_     _-_
_-_     _-_     -_-_-     _-_     _-_
_-_     _-_  _-_-   -_-_  _-_     _-_
_-_-_-_-_-_  _-_-   -_-_  _-_-_-_-_-_
_-_-_-_-_-_  _-_-   -_-_  _-_-_-_-_-_
        _-_  _-_-   -_-_          _-_
        _-_  _-_-   -_-_          _-_
        _-_  _-_-   -_-_          _-_
        _-_     -_-_-             _-_
        _-_      _-_              _-_
}
2
  • 15
    Seems like the second line can be replaced wth int f,o,ur = 0,h = f+o+ur;. Is it legal to omit the int as well? Commented Aug 15, 2014 at 11:19
  • 1
    @immibis: Simply omitting int is illegal in any standard C. At least some decl-specifier is required, meaning that you can replace int with static or const, but you can't just leave it blank. Plain main() is illegal in standard C for the same reason. It can be extern main(), but not just main(). Commented Feb 12, 2018 at 8:37
56

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

1
  • 11
    It is actually from the official 404 page on SO now.
    – jjnguy
    Commented Nov 5, 2009 at 0:15
38

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.

0
29

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()
3
  • 14
    might as well finish up that TODO Commented Sep 18, 2015 at 2:06
  • 1
    "This program takes advantage of the fact that C requires functions without an explicit return type to be int." Modern C no longer supports the "implicit int rule". And even the original C89/90, which supported "implicit int" (and implicit function declarations), did not allow empty decl-specifier-seq in declarations. So, plain main() is illegal in any standard C. Commented Feb 12, 2018 at 8:42
  • 3
    You'll never finish that TODO, because the code does not actually work in Befunge-93.
    – Jo King
    Commented Mar 1, 2018 at 0:51
19

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
0
15

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
3
  • Here is an online interpreter for whitespace.
    – Cemafor
    Commented Feb 26, 2013 at 17:02
  • Maybe the newlines/some copy/paste artifact is affecting it, but that gives a syntax error for me
    – Zelda
    Commented Feb 26, 2013 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
    Commented Feb 27, 2013 at 5:17
-10

This code using Python, C test OK!

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

Py3 404

https://repl.it/FK9L/0

C 404

https://repl.it/FK8v/0
https://repl.it/FK8v/1

enter image description here enter image description here

You must log in to answer this question.

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