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.

Produce only the name of a programming language using only the facilities of the language itself. The name of the language can only be invoked indirectly, i.e., from non custom environment variables, built-in functions or methods or the interpreter/compiler of the language. Output should be the name of the language only.

One example would be:

$ awk --v | awk 'NR<2 { print $2}'  # GNU Awk 3.1.8
Awk

The answer I select will be the one with the most up votes. In case of a tie, the shortest golfed version (separate or the only given answer), will be the tie breaker.

share|improve this question
    
so using Java reflection API is not OK? –  SHiNKiROU Feb 21 '11 at 1:15
    
I didn't think of that, but that's okay too. –  Ty Auvil Feb 21 '11 at 12:20
    
@George: Huh? Is that now a code-golf? Why? It wasn't before ... –  Joey Feb 21 '11 at 19:03
    
@Joey: No it wasn't before, but it seemed like a code golf question, so I edited it and someone approved the edit. –  Nathan Osman Feb 21 '11 at 19:35
    
@Joey: Code Golf is the tie breaker. I didn't put that TAG on originally, but George suggested it and I agreed. –  Ty Auvil Feb 21 '11 at 23:11
show 6 more comments

84 Answers

up vote 17 down vote accepted

BASH, 17 7

bash -c 'echo $0'

or even

echo ${0#-}

if you already run bash :D

update: "echo $0" gets $0 expanded first so 'echo $0' is the correct answer.

share|improve this answer
    
It's only dash-prefixed if it's a login shell and it's not a login shell if it's the one that runs your program. –  J B Feb 22 '11 at 20:02
    
@JB: thanks for pointing that out. I was using "s where I should have 's –  Eelvex Feb 22 '11 at 20:10
    
Looks like this is the winner. :) –  Ty Auvil Feb 28 '11 at 23:59
    
echo ${SHELL:5} –  fossilet May 18 '12 at 9:27
add comment

Whitespace

|  	 	 			
	
    		 	   
	
    		 	  	
	
    			 	  
	
    		  	 	
	
    			  		
	
    			    
	
    		    	
	
    		   		
	
    		  	 	
	


|
share|improve this answer
    
Not a real language, whitespace. –  Alexandru Jul 2 '11 at 0:11
15  
@Alexandru: compsoc.dur.ac.uk/whitespace –  dmckee Jul 2 '11 at 0:57
    
Argh. Sorry Anthony, SE doesn't let me upvote you now. –  Alexandru Jul 2 '11 at 14:10
    
That's all right. I myself have recently learned about this language. –  Anthony Jul 2 '11 at 19:49
1  
If I'm not mistaken, your program has two fatal errors: (1) Letting S,T,L denote Space,Tab,Linefeed repsectively, the first instruction is at present SSTSTSTTTL, but should be SSSTSTSTTTL (3 S's on the left) -- the 3rd S is needed to indicate the positive sign of the binary number following it (i.e. the ascii code for 'W' = 85 = 1010111 in binary, coded as the L-terminated string TSTSTTTL), and so on for the other ascii codes. (2) The prog's final TLLL should be TLSSLLL, where the TLSS is needed to output the final character from the stack, and LLL marks the end-of-program. –  r.e.s. Mar 29 '12 at 0:56
show 2 more comments

C

#define d(x) x(#x[3])
void main(){d(putchar);}
share|improve this answer
    
putchar((int)(putchar)+3); –  muntoo Mar 12 '11 at 23:04
12  
putchar("putchar"[3]); I must have been an idiot an year ago. –  muntoo Apr 1 '12 at 1:50
add comment

J,4

u:74

Having a short language name does kind of help.

share|improve this answer
    
Can be even shorter : u:74 –  jpjacobs Feb 24 at 12:13
add comment

Python - 23 22

print`credits`[97:103]
share|improve this answer
2  
you should leave out the space after print –  gnibbler Jul 5 '11 at 6:25
    
What does the ` do? I've never seen that. –  Kazark Jan 1 '12 at 4:13
3  
Backticks are a deprecated alias for repr(). Backticks are gone in Python 3.x. –  0xKirill Jan 4 '12 at 9:49
    
THAT IZ AWSUM :) –  Soham Chowdhury May 17 '13 at 10:08
    
@0xKir So? Use them in code golf 'till they're gone. –  Riking May 24 '13 at 14:34
add comment

Python (win32) - abuse of the random module

I'm not sure whether this actually works. Depends on the implementation.

print ''.join([(random.seed(835)==None)and'']+[chr(random.randint(64,90)) for x in range(3)]+[(random.seed(53281)==None)and'']+[chr(random.randint(64,90)) for x in range(3)])
share|improve this answer
    
How did you find that? It also works for me in Python 2.6.6 on linux. –  Mechanical snail Aug 5 '11 at 2:42
4  
Good to hear! I wrote a small bruteforcing tool to determine how you need to seed the random module to get a certain sequence. With a three-character sequence, this doesn't take too long - this script joins together two three-letter sequences to get "PYTHON" :) –  zhazam Aug 15 '11 at 13:24
    
The rest is using list comprehensions and other dirty tricks to get it to fit it in one line. You can find the bruteforcing tool here :) –  zhazam Aug 15 '11 at 13:30
    
And it works on my Python 3.1.2 (adding parentheses after print, of course). –  Mechanical snail Aug 16 '11 at 20:18
1  
Almost there: generateSentence(50613, 73)[66:] gives 'PYTHOCN'. –  Mechanical snail Aug 16 '11 at 20:45
add comment

Python

import sys;print sys.copyright[24:30]

This also works for me (sys.executable is '/usr/bin/python')

import sys;print sys.executable[9:]
share|improve this answer
1  
Here is what i get from these: Active and 25\Lib\site-packages\pythonwin\Pythonwin.exe, so... no good –  Nas Banov Mar 7 '11 at 8:12
    
import sys;print sys.subversion[0][1:] works though –  Nas Banov Mar 7 '11 at 8:19
5  
that's cheating... But I can cheat better: eval.__doc__[144:150] –  JBernardo Jul 5 '11 at 5:53
add comment

brainfuck 101

++++++++[->++++++++++++>+>>++>+++<<<<<]>[->+>+>+>+<<<<]>+>++.>++.<-.<.>>----.<<---.>>>---.<<++.>---.

could probably be golfed slightly further, but it's 6am and I should get some sleep...

share|improve this answer
add comment

C

In the spirit of Anon's answer in C (considering a C file always has the extension .c). I'd suggest :

#include <stdio.h>
void main(){puts(__FILE__+sizeof(__FILE__)-2);}
share|improve this answer
5  
Much shorter, 25 chars, single line, main(){puts(__FILE__+2);}, assuming filename is a single letter, eg. a.c –  Hasturkun Feb 24 '11 at 15:09
add comment

Piet, 47x2 pixels

Using 5x5 codels: Piet program to print "Piet"

share|improve this answer
add comment

BrainFuck

Just for fun (I know this isn't related to the purpose of the task.), I wrote this code...

+++++[><-+-+-><><>++<><>+++++<>+<]>>>+
+>><<+<>+><<>+[><-<<+>>><>+<><>++<]>[-<<<
+>>>]+<<+<[><->>+>><>+<<><+<<]>->-><>->
+++++<<>+++++<<>------>><>+.<<[-<>+<>->
+<]>>+><+<>><+><+[<]>-<><>-><<>-<<>+>-.<
+<><>+<>+[><>-<>-><<>-<>><-<><>-><><-+<
++---]>[+>]<<+><+.>><-<<+++++[>+<><><-]
>
+++.>+<+<>+++<>+.+>
-.<><+<+><+><><++>
+++<>+<+>>+<><>+<
+.<><+[->>+>><<++
+<>><++-<<-+-<>+]+
>
>
---.<
---
---
---
<>>
<-.
---
>-<

And when I replaced <,>,[,],. with space...

+++++   -+-+-     ++    +++++  +     +
+    +  +    +   -  +     +    ++    -   
+    +  +    -  +    +    +    - -   - 
+++++   +++++   ------    +    -  +  - 
+    +  +    +  +    -    -    -   + -  
+    +  +    -  -    -    -    -    -+ 
++---   +    +  +    -  +++++  +     - 

+++  + +  +++  + + 
-    + +  +    ++ 
+++  + +  +    + 
+    + -  +    ++
+    ++-  -+-  + +


---  
---
---
---

 - 
---
 - 
share|improve this answer
add comment

C

Built using DevStudio 2005

#include <stdio.h>
void main ()
{
  int i,j,i2;
  for (i=j=0,i2=200;i2<=200;i2-=19,j==putchar(32|(i2<'Q'&&i2>'+'&&(j<20||i<12||i>28))*3)+8?j=0,i2+=i,i+=putchar('\n')-8:(i2+=j,j+=2));
}

Slightly shorter version (with compiler warnings)

int i,j,k;
for (i=j=k=0;k<=0;k-=19,j==putchar(32|(k<-119&&k>-157&&j<20|i<12|i>28)*3)+8?j=0,k+=i,i+=putchar('\n')-8:(k+=j,j+=2));
share|improve this answer
    
+1 for creative output –  Nathan Osman Feb 22 '11 at 5:18
    
first one will produce warning about main not being of type int using gcc. –  oenone Aug 16 '11 at 7:28
1  
You are not allowed to use the letter 'c'. –  kinokijuf Dec 25 '11 at 11:29
add comment

Java

public class Name {
    public static void main(String[] args) {
        String s = "";
        s = s.getClass().getName();
        s = s.substring(0, 1).toUpperCase() + s.substring(1, 4);
        System.out.println(s);
    }
}
share|improve this answer
    
anon: Just submit another answer. This isn't a golf, so no need to keep it short. –  Joey Jul 1 '11 at 7:44
add comment

Ruby - 27

puts RUBY_DESCRIPTION[0..3]

Update from Chris Jester-Young's comment:

Ruby - 24

puts RUBY_COPYRIGHT[0,4]

Updated from Hauleth:

Ruby - 15

p`ruby -v`[0,4]
share|improve this answer
    
1. RUBY_COPYRIGHT is shorter than RUBY_DESCRIPTION. 2. [0,4] is shorter than [0..3]. –  Chris Jester-Young Mar 11 '11 at 8:56
    
Even shorter ```ruby -v`[0,4]`` –  Łukasz Niemier Mar 27 '12 at 21:30
add comment

cat 3

Create a file with the following content (source code)

cat

And run it like (execute the file):

$ cat filename
share|improve this answer
7  
Neither indirect nor a language? –  eternalmatt Jul 22 '11 at 22:35
4  
Cat isn't a real language, because it isn't Turing-complete. –  kinokijuf Dec 25 '11 at 11:36
3  
@kinokijuf There are plenty of useful languages which are not Turing-complete. See stackoverflow.com/q/8412741/20713 for a partial list. –  ephemient Mar 27 '12 at 6:43
5  
@ephemient: Cat is not only not turing complete, it isn't a language at all. It has no syntax, no grammer, no keywords. I don't know where bitmask got his definition - I don't follow it. –  user unknown Mar 27 '12 at 10:55
6  
@userunknown It's hard to imagine cat being a language, and besides this violates the spirit of the question (uses the language name directly). My comment was to refute the idea that "Cat isn't a real language, because it isn't Turing-complete". It isn't a language for other reasons, but Turing-completeness isn't a good deciding factor. –  ephemient Mar 28 '12 at 4:08
show 2 more comments

Clojure - 16 chars

(subs(str =)0 7)
share|improve this answer
add comment

Brainfuck

>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++++++++++++++++++++++++++++++++.
-----------------.
++++++++.
+++++.
--------.
+++++++++++++++.
------------------.
++++++++.
share|improve this answer
    
Br**n ! (five more to go ...) –  belisarius Feb 21 '11 at 18:38
    
@belisarius: Hm? –  Joey Feb 21 '11 at 19:02
    
Br**n you. –  muntoo Mar 10 '11 at 4:46
1  
you can make smaller with [...]: ++++++++++[>++++++>++++++++++>+++++++++++>++++<<<<-]>++++++.>>++++.<---.++++++++‌​.+++++.--------.>>++.*<<---.++++++++. –  JBernardo Jul 7 '11 at 0:27
11  
This looks like typical Git output. –  Joey Adams Jul 7 '11 at 3:34
show 4 more comments

Scala — 42

print(((1,1).getClass+"").substring(6,11))

Update — 39 chars

print(('a.getClass+"").substring(6,11))
share|improve this answer
    
Nice! A variant, same size: print((""+'a.getClass).split("\\b")(3)) –  PhiLho Jul 27 '11 at 16:00
    
I prefer it capitalized, but of course, it isn't golf anymore. So, for the pleasure of making variants: print(classOf[App].toString.split("\\W").map(_.capitalize).apply(1)) –  PhiLho Jul 28 '11 at 8:23
    
Same trick in 23 chars: (""+Map).patch(5,"",99) –  deadsven Oct 25 '11 at 11:51
    
And 16: (""+Map).take(5) –  deadsven Oct 25 '11 at 12:04
    
Or 13: ""+Map take 5 –  Régis Jean-Gilles Jan 26 '13 at 1:14
add comment

C - 34

I don't have enough reputation to add this as a comment to the previous C entry. Same idea as above, but the filename can be anything.c

main(){puts(index(__FILE__,0)-1);}
share|improve this answer
2  
Gah, but now I can't name my files "&raquo;\0<3ASCII.c" –  walpen Jun 13 '12 at 0:50
add comment

><> (Fish) (11)

Even better:

'>'::o2-oo;
share|improve this answer
add comment

Ruby (17)

p 1299022.to_s 36

Outputs "ruby"

share|improve this answer
add comment

Visual Basic (version 6.0)

MsgBox Right(Error(458), 12)

VBScript

On Error Resume Next
Err.Raise 458
MsgBox Right(Err.Description, 8)

Both VB6 and VBScript

On Error Resume Next
Err.Raise 458
D = Err.Description
MsgBox Mid(D, InStr(2, D, "V"))
share|improve this answer
    
The VBScript one produces "Bscript." on my computer. –  kinokijuf Dec 25 '11 at 11:32
    
You're a bad man. –  Riking May 24 '13 at 14:39
add comment

QBasic (37)

I am using version 1.1 of the QBasic interpreter.

FOR x=0TO 5:?CHR$(PEEK(2588+x));:NEXT
share|improve this answer
add comment

Perl

$^X=~/([a-z]+)[^\/]+$/;print$1,$/
share|improve this answer
    
I get only per as output (missing the final l). –  BioGeek Jul 2 '11 at 22:49
1  
@BioGeek: Is your Perl binary called per? –  ninjalj Jul 3 '11 at 6:42
1  
I also get per. (My $^X says /usr/bin/perl. The [^\/]+ swallows the l.) –  Ilmari Karonen Dec 12 '11 at 22:35
    
fixed: $^X=~/([^\/]+)$/;print$1, run with -l command line switch –  ardnew Apr 30 '12 at 21:34
add comment

Lua 25 Chars

print(arg[-1]:match"%w+")

Or, analogue to the J B's J solution 17 chars:

print"\76\117\97"
share|improve this answer
1  
_VERSION:sub(1,3) –  mniip May 17 '13 at 15:59
add comment

GolfScript (13)

"#{$0[0..9]}"
share|improve this answer
    
+1 Clever... very clever. Of course, this doesn't work if you've renamed the interpreter, or if you're, say, running it from a different directory. –  Ilmari Karonen Mar 30 '12 at 11:13
    
Ps. "#{$0}"10< is three chars shorter. :) –  Ilmari Karonen Mar 30 '12 at 11:20
add comment

Racket (45)

racket -e "(display(substring(banner)10 17))"
share|improve this answer
add comment

D(52)

using the extension of the source file

import std.stdio;void main(){write(__FILE__[$-1]);}
share|improve this answer
add comment

Haskell, 47

data H=Haskell deriving Show
main=print Haskell
share|improve this answer
add comment

Ruby

Not short, but very indirectly.

4.times{|x|print((82+11.24*x+8.231*Math.tan(111.2*x)).floor.chr)}
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.