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()
# - 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