polyglot programming
by fledgling otakuvia a comment thread at Good Math, Bad Math comes this polyglot program that compiles in 8 different programming languages:
- COBOL (ANSI)
- Pascal (ISO)
- Fortran (ANSI, f77)
- C (ANSI-ish)
- PostScript
- Linux/Unix shell script (bash, sh, csh)
- x86 machine language (MS-DOS, Win32, Linux)
- Perl (version 5)
Hideous and elegant simultaneously. All it does is write, “Hello, polyglot.” to stdout. The trick is clever use of comment strings.
Why yes, it is indeed 4am and the baby has been keeping us awake forever.


April 28th, 2007 at 4:01 am
There was a C program I saw about 30 years ago which, when compiled and run, would print out its own source.
That’s a hell of a lot tougher than it sounds. The trick in that case was careful (ab)use of macros.
April 28th, 2007 at 4:07 am
Steven, that is called a _quine_ - check out “the original post”:http://scienceblogs.com/goodmath/2007/04/true_pathology_a_multilingual.php and comments at Good Math, Bad Math for many more examples. There’s even an entire programming language where “the only way to loop is to quine the program”:http://scienceblogs.com/goodmath/2006/11/friday_pathological_programmin_5.php! Cool but esoteric
Here’s a link to quines in all the major programming languages. And here’s a simple quine in C:
#include%cmain(){char*c=%c%c%c%.102s%cn%c; main(){char*c=”\\\”#include
printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}\n”;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}