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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

closed as unclear what you're asking by Dennis Jan 31 at 3:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

7  
The technical term being Polyglot – st0le Jan 2 '12 at 11:55
7  
Here's 16 language polyglot – st0le Jan 2 '12 at 11:56
7  
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
7  
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
3  

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

5 languages- Thue, Brainf***, Boolf***, Treehugger, and Javascript

/*::=
alert::=~This is Thue!
::=
-><[[--->+<]>-.[---->+++++<]>-.+.++++++++++.+[---->+<]>+++.-[--->++<]>-.++++++++++.+[---->+<]>+++.+[->++<]>.---[----->+<]>-.+++[->+++<]>++.++++++++.+++++.--------.---[->+++<]>+...---------.[-]]
^^[[--->+^]>-.[---->+++++^]>-.+.++++++++++.+[---->+^]>+++.-[--->++^]>-.++++++++++.+[---->+^]>+++.>-[--->+^]>-.-[--->+^]>+.-------------..+++.[--->+^]>---.++[->+++^]>++..--.+++++++++++++.[--->+^]>-----.[-]]
-+[+;;+;+;+;+;+;+;;;;+;+;+;;+;+;+;;+;+;+;;+;+;;+;;+;;;+;;;;;;+;+;;+;+;;+;+;+;;+;+;;+;;+;;;+;;;;;;+;+;;;+;+;;;;+;+;+;;;;+;+;;+;+;;;;+;+;;+;;;+;;+;+;;+;;+;;+;;+;;+;;+;+;+;+;+;+;;;+;+;+;+;+;+;;;+;+;+;+;+;+;;+;+;;;;+;+;;]
*/alert("This is Javascript!")

Note that the Treehugger portion times out in the web-based implementation, unfortunately, so you should use a different Treehugger interpreter.

share|improve this answer

protected by Timtech Jun 12 '14 at 19:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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