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

1 Answer 1

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");}
import builtins
print = lambda : builtins.print("Hello world! -- from Python")


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

protected by Timtech Jun 12 '14 at 19:58

Thank you for your interest in this question. Because it has attracted low-quality answers, 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.