Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Excluding trivial programs, what code compiles in the most number of languages?

(By "trivial" I mean to exclude answers such as the empty program or text that will be echoed directly.)

The following code apparently compiles in all of the following programming languages (and prints something different in each one): C, C++, Perl, TeX, LaTeX, PostScript, sh, bash, zsh and Prolog.

%:/*:if 0;"true" +s ||true<</;#|+q|*/include<stdio.h>/*\_/
{\if(%)}newpath/Times-Roman findfont 20 scalefont setfont(
%%)pop 72 72 moveto(Just another PostScript hacker,)show((
t)}. t:-write('Just another Prolog hacker,'),nl,halt. :-t.
:-initialization(t). end_of_file. %)pop pop showpage(-: */
int main(){return 0&printf("Just another C%s hacker,\n",1%
sizeof'2'*2+"++");}/*\fi}\csname @gobble\endcsname{\egroup
\let\LaTeX\TeX\ifx}\if00\documentclass{article}\begin{doc%
ument}\fi Just another \LaTeX\ hacker,\end{document}|if 0;
/(J.*)\$sh(.*)"/,print"$1Perl$2$/"if$_.=q # hack the lang!
/
sh=sh;test $BASH_VERSION &&sh=bash;test $POSIXLY_CORRECT&&
sh=sh;test  $ZSH_VERSION && sh=zsh;awk 'BEGIN{x="%c[A%c[K"
printf(x,27,27)}';echo "Just another $sh hacker," #)pop%*/

That's 10 different languages. I found it via pts oldalai (who also has a magnificent Christmas poem written in C, C++, Perl and TeX). Can anyone do better?

share|improve this question
6  
The technical term being Polyglot –  st0le Jan 2 '12 at 11:55
4  
Here's 16 language polyglot –  st0le Jan 2 '12 at 11:56
6  
If sh, bash, and zsh count as different languages even when it's essentially only using sh then I think you need to specify precisely what you count as different languages. E.g. Perl 4 vs Perl 5.10 have some significant differences. –  Peter Taylor Jan 2 '12 at 13:14
5  
There are languages (e.g. Whitespace, BrainF**k and Perl), where pretty much any character combination is a valid program. They can be claimed by any program. –  ugoren Nov 18 '12 at 19:29
2  
show 5 more comments

1 Answer

3 Languages -- C, C++, and Python

#ifdef _cplusplus
    #include <iostream>
    #define print() int main(){cout << "Hello world! -- from C++" << endl;}
#elif (defined __STDC__) || (defined __STDC_VERSION__)
    #include <stdio.h>
    #define print() int main(){printf("Hello world! -- from C\n");}
#else
import builtins
print = lambda : builtins.print("Hello world! -- from Python")
#endif

print()

Something different is printed in each language. In C & C++, lines starting with '#' are preprocessing directives, but those same lines are comments in Python.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.