The challenge
You are given a function compile(s: String): Integer which returns 0 if s could be compiled and anything other than 0 if it failed (the resulting code is not important).

The thing is that you don't know for what language the function is made for, so it is up to you to figure that out by giving the function small snippets of code and observing what it returns. The good part is that a team of scientists have tried before you and have narrowed down the list to C, C++, C#, Java and JavaScript (these are the only widespread languages which have similar syntax I could think of).


  • General code-golf rules apply.
  • There is no strict output format; as long as it's readable and understandable it's ok.
  • If these 5 languages are not enough for you then you can add your own favourites and you'll be rewarded with extra stars (★) (that do nothing).

Note1: the compiler is up to date with the latest standard of whatever language it's designed for.
Note2: I know JavaScript does not compile, but it goes though some parsing process; if it's valid JavaScript code then it passes

  • 2
    \$\begingroup\$ What are we to provide? The set of strings that can be used to tell the compilers apart, minimizing the total length of the strings? \$\endgroup\$ – Geoff Reedy Jan 11 '13 at 3:50
  • \$\begingroup\$ @GeoffReedy I was looking just for the code to determine the language, but the explanations you provided are more than welcome \$\endgroup\$ – adrianton3 Jan 11 '13 at 7:23

Total snippet length 8; test program 83 (C)

Distinguish JavaScript and C from C++, Java, and C#


Distinguish JavaScript from C


Distinguish C++ from C# and Java


Distinguish C# and Java


C code to determine the language

#define C(x)compile(#x)?

Snippets tested with

C: gcc 4.7.2 with -std=c99
C++: g++ 4.7.2 with -std=c++11
Java: javac 1.7.0_09
JavaScript: JavaScript-C 1.8.5
C#: Mono C# compiler version

  • \$\begingroup\$ #define C(x)compile(#x)? saves a space and lots of quotes. \$\endgroup\$ – ugoren Jan 11 '13 at 13:46
  • \$\begingroup\$ @ugoren thanks, I actually thought I tried the stringification thing but it didn't work for some reason; I must have had some other problem too, cause it works fine now \$\endgroup\$ – Geoff Reedy Jan 11 '13 at 13:58
  • \$\begingroup\$ But you somehow got it mixed up - C(#x) won't work. \$\endgroup\$ – ugoren Jan 11 '13 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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