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.

Using your language of choice, golf a quine.

No cheating -- that means that you can't just read the source file and print it. Also, in many languages, an empty file is also a quine: that isn't considered a legit quine either.

Points for:

  • Smallest code (in characters)
  • Most obfuscated/obscure solution
  • Using esoteric/obscure languages
  • Successfully using languages that are difficult to golf in

Also, sorry about the title. I was just going to make it quine but it has to be at least 15 characters.

share|improve this question
1  
Do you not mean, "Golf you a quine for greater good!"? And, yes, I'm a 'Grammar Nazi'. –  muntoo May 3 '11 at 2:49
10  
@muntoo it's a play on "Learn you a Haskell for Great Good". –  Rafe Kettler May 3 '11 at 2:52
3  
what about random text generator quines (a probability that the program will print itself) –  SHiNKiROU May 14 '11 at 0:01
add comment

62 Answers

Fine, here's a 1-character quine in GolfScript:

1
share|improve this answer
10  
Or PowerShell, or PHP :-) –  Joey Jan 28 '11 at 9:54
2  
You didn't go back in time and give the inventor the idea to invent GolfScript, did you? –  muntoo May 3 '11 at 2:38
42  
Technically, 1 is not a quine in GolfScript: it outputs 1\n, where \n denotes a newline. However, the two-char program 1\n is a quine. –  Ilmari Karonen Feb 3 '12 at 8:27
5  
The one-char program \n probably also is? –  nooodl Aug 23 '13 at 14:51
    
No, Ilmari, even that is not a quine. A quine is not just a program which prints its own source, it has to have a specific structure to be a quine. –  Pseudonym Feb 12 at 5:59
add comment

MySQL, 167 characters

SELECT REPLACE(@v:='SELECT REPLACE(@v:=\'2\',1+1,REPLACE(REPLACE(@v,\'\\\\\',\'\\\\\\\\\'),\'\\\'\',\'\\\\\\\'\'));',1+1,REPLACE(REPLACE(@v,'\\','\\\\'),'\'','\\\''));

That's right. :-)

I really did write this one myself. It was originally posted at my site.

share|improve this answer
add comment

HQ9+ (1 character)

Not quite as short as the 0 char one, but here it is anyway:

Q
share|improve this answer
6  
This one is legit... –  st0le Feb 6 '11 at 11:51
    
What's great about this one is unlike others (like the 1 in golfscript or php) is that you can have any arbitrary HQ9+ program (e.g., H+9+H9) and the quine of that program is always 1 more character than the program itself (in this case H+9+H9Q). –  Casey Apr 23 '11 at 21:17
4  
@Casey that's not entirely true: you can never have an HQ9+ Quine that doesn't involve exactly 1 Q and some sequence of +s (it is a stretch to say that the second program you have there "is a quine of" the first - they are different, and the "Quine" you suggested outputs too much - the lyrics to 99 bottles occur twice in the programs output, but not in the source) –  tobyodavies Apr 24 '11 at 16:31
add comment

PostScript, 20 chars

Short and legit. 20 chars including trailing newline.

(dup == =)
dup == =
share|improve this answer
add comment

If we're going for short quines:

0 characters.

share|improve this answer
12  
Points for originality, but I'm adding this to the list of "ways to cheat". –  Rafe Kettler Jan 28 '11 at 0:39
22  
@Rafe: it's not very original. This was one of the winners of the 1994 International Obfuscated C Code Contest: the smallest self reproducing program: de.ioccc.org/years-spoiler.html#1994_smr –  R. Martinho Fernandes Jan 28 '11 at 2:18
8  
Can we avoid up-voting silly answers? –  marcog Jan 28 '11 at 11:26
15  
@marcog what is code golf but answering silly questions with rediculous (or rediculously complex) answers ;) –  tobyodavies Jan 28 '11 at 12:01
9  
@toby It's called common sense. If we don't apply it, this site will devolve into a rather stupid one. Up-vote the clever, but still short questions not the ones that required half a brain cell to come up with. –  marcog Jan 28 '11 at 12:09
show 6 more comments

Brainf*ck (755 characters)

This is based off of a technique developed by Erik Bosman (ejbosman at cs.vu.nl). Note that the "ESultanik's Quine!" text is actually necessary for it to be a quine!

->++>+++>+>+>++>>+>+>+++>>+>+>++>+++>+++>+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+>++>>>+++>>>>>+++>+>>>>>>>>>>>>>>>>>>>>>>+++>>>>>>>++>+++>+++>+>>+++>>>+++>+>+++>+>++>+++>>>+>+>+>+>++>+++>+>+>>+++>>>>>>>+>+>>>+>+>++>+++>+++>+>>+++>+++>+>+++>+>++>+++>++>>+>+>++>+++>+>+>>+++>>>+++>+>>>++>+++>+++>+>>+++>>>+++>+>+++>+>>+++>>+++>>>+++++++++++++++>+++++++++++++>++++++>+++++++++++++++>++++++++++>+++>+++>++++>++++++++++++++>+++>++++++++++>++++>++++++>++>+++++>+++++++++++++++>++++++++>++++>++++++++++++>+++++++++++++++>>++++>++++++++++++++>+++>+++>++++>++++++>+++>+++++++++>++++>+>++++>++++++++++>++++>++++++++>++>++++++++++>+>+++++++++++++++>+++++++++++++
ESultanik's Quine!
+[[>>+[>]+>+[<]<-]>>[>]<+<+++[<]<<+]>>+[>]+++[++++++++++>++[-<++++++++++++++++>]<.<-<]
share|improve this answer
4  
That's a clever way to do it. –  Peter Olson Jun 22 '11 at 16:48
add comment

Python (29)

_='_=%r;print _%%_';print _%_

from en.literateprograms.org

share|improve this answer
add comment

Cross-browser JavaScript (41 characters)

It works in the top 5 web browsers (IE >= 8, Mozilla Firefox, Google Chrome, Safari, Opera). Enter it into the developer's console in any one of those:

eval(I="'eval(I='+JSON.stringify(I)+')'")

It's not "cheating" — unlike Chris Jester-Young's single-byte quine, as it could easily be modified to use the alert() function (costing 14 characters):

alert(eval(I="'alert(eval(I='+JSON.stringify(I)+'))'"))

Or converted to a bookmarklet (costing 22 characters):

javascript:eval(I="'javascript:eval(I='+JSON.stringify(I)+')'")
share|improve this answer
add comment

java, 530 bytes:

A java-Solution with an original approach:

import java.math.*;class a{public static void main(String[]a){BigInteger b=new BigInteger("90ygts9hiey66o0uh2kqadro71r14x0ucr5v33k1pe27jqk7mywnd5m54uypfrnt6r8aks1g5e080mua80mgw3bybkp904cxfcf4whcz9ckkecz8kr3huuui5gbr27vpsw9vc0m36tadcg7uxsl8p9hfnphqgksttq1wlolm2c3he9fdd25v0gsqfcx9vl4002dil6a00bh7kqn0301cvq3ghdu7fhwf231r43aes2a6018svioyy0lz1gpm3ma5yrspbh2j85dhwdn5sem4d9nyswvx4wmx25ulwnd3drwatvbn6a4jb000gbh8e2lshp",36);int i=0;for(byte c:b.toByteArray()){++i;if(i==92)System.out.print(b.toString(36));System.out.print((char)c);}}}

in readable form:

import java.math.*;
class a
{
    public static void main (String [] a)
    {
        BigInteger b=new BigInteger ("90ygts9hiey66o0uh2kqadro71r14x0ucr5v33k1pe27jqk7mywnd5m54uypfrnt6r8aks1g5e080mua80mgw3bybkp904cxfcf4whcz9ckkecz8kr3huuui5gbr27vpsw9vc0m36tadcg7uxsl8p9hfnphqgksttq1wlolm2c3he9fdd25v0gsqfcx9vl4002dil6a00bh7kqn0301cvq3ghdu7fhwf231r43aes2a6018svioyy0lz1gpm3ma5yrspbh2j85dhwdn5sem4d9nyswvx4wmx25ulwnd3drwatvbn6a4jb000gbh8e2lshp", 36); 
        int i=0; 
        for (byte c:b.toByteArray ())
        {
            ++i; 
            if (i==92) 
                System.out.print (b.toString (36)); 
            System.out.print ((char) c);
        }
    }
}
share|improve this answer
add comment

These are the two shortest Ruby quines from SO:

_="_=%p;puts _%%_";puts _%_

and

puts <<2*2,2
puts <<2*2,2
2

Don't ask me, how the second works...

share|improve this answer
7  
The second one uses heredoc, <<2 starts a string on the next line, and *2 repeats the string –  SHiNKiROU Jan 28 '11 at 2:52
add comment

PHP, 32

Probably not an optimal solution, but it makes me smile.

HAHA THIS IS TECHNICALLY A QUINE

Give it a shot, works like a charm.

share|improve this answer
1  
+1 just because I laughed. :P –  user1354557 Dec 11 '13 at 23:33
1  
+1 for "give it a shot" :-DD –  Tomas Feb 1 at 14:51
add comment

Unix script (11 chars)

I am not exactly sure if this disqualifies as You can't just read the source file and print it, but it certainly is a nice one. Taken from http://research.swtch.com/zip.

#!/bin/cat
share|improve this answer
2  
+1 well technically "you" don't read the source, cat does –  Tim Seguine Feb 1 at 15:24
add comment

Python (29)

Just for fun: :)

SyntaxError: invalid syntax

I actually posted this because I wanted to post some (prehistoric) credit where it's due :p

share|improve this answer
1  
+1 for the idea :-)) –  Tomas Feb 1 at 17:53
add comment

Python 2.7, 14 chars

print __file__

Name the file print __file__ (DON'T add the .py) and run it with your interpreter!

it should work even without the .py in the name

share|improve this answer
    
Stupid question: does it require chmod u+x on UNIX-like systems? Since we're talking about the extension being optional. –  Arlaud Pierre Dec 20 '13 at 13:24
    
@ArlaudPierre No, it doesn't require +x. –  heinrich5991 Jan 5 at 15:56
add comment

Fob (135)

If Fob, a language of my own creation some time ago, I present a rather interesting 135 byte quine:

$$#<&$::#<&$:#<&#<&$:#<=#<&$&//%<//<.&%<<%.%<&>/////%<<%.<&.%<.%/////<&.%<<&/.%%<&>%</%<////<&.%<<%/<&.%%<&>/%//<&.%<</&.%%%<&>>/>>#<=
share|improve this answer
add comment

Javascript (36 char)

(function a(){alert("("+a+")()")})()

This is, AFAICT, the shortest javascript quine posted so far.

share|improve this answer
    
That... is impressive. You should explain how it works for me 8-| –  TehShrike Sep 27 '11 at 19:18
3  
@TehShrike Hint: you can view the contents a function by coercing it to a string. For example, if you have a function a, you can access its contents by calling a.toString. –  Peter Olson Sep 27 '11 at 19:22
6  
To be pedantic, though, this is only a quine if your JavaScript implementation stringifies the function a exactly the same way as it's been written above. However, the output of this code is likely to be a quine on any JavaScript implementation. –  Ilmari Karonen Feb 3 '12 at 18:55
    
@IlmariKaronen not exactly. If an other implementation chooses to add a space between the arguments and the body, the output is not a quine in an implementation that doesn't add a space between the arguments and body. –  Johannes Kuhn Dec 21 '13 at 20:59
    
@JohannesKuhn: I meant that, for any given JavaScript implementation, the output of running the above code on that implementation is likely to be a quine on that implementation. Sorry for the imprecise language. –  Ilmari Karonen Dec 21 '13 at 21:22
show 1 more comment

A Classic - Lisp - 78

((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x))))

A beautiful snippet, but give credit where credit is due.

share|improve this answer
2  
Actually this code returns itself instead of printing itself. Running it in an interpreter with read-eval-print loop will of course print the returned list, but the printing is not part of the code itself. The C equivalent of this would be a C code which outputs its executable instead of its source code. Which would certainly also be a quite interesting problem, although heavily system-dependent. –  celtschk Feb 3 '12 at 16:13
add comment

JavaScript 2688!!

Any awards for the most abysmal quine? My first go, it builds a little html interface which lets your run it again to build another interface which lets you... you get it, try it here

var a = [47, 42, 32, 90, 108, 97, 116, 97, 110, 39, 115, 32,
        115, 101, 108, 102, 32, 114, 117, 110, 110, 105, 110,
        103, 32, 113, 117, 105, 110, 101, 32, 42, 47, 10,
        118, 97, 114, 32, 116, 97, 59, 10, 10, 102, 117,
        110, 99, 116, 105, 111, 110, 32, 102, 119, 114, 105,
        116, 101, 40, 97, 114, 114, 41, 32, 123, 10, 9,
        118, 97, 114, 32, 107, 32, 61, 32, 39, 39, 59,
        10, 9, 118, 97, 114, 32, 98, 117, 116, 32, 61,
        32, 100, 111, 99, 117, 109, 101, 110, 116, 46, 99,
        114, 101, 97, 116, 101, 69, 108, 101, 109, 101, 110,
        116, 40, 39, 73, 78, 80, 85, 84, 39, 41, 59,
        10, 9, 118, 97, 114, 32, 98, 111, 32, 61, 32,
        100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101,
        116, 69, 108, 101, 109, 101, 110, 116, 115, 66, 121,
        84, 97, 103, 78, 97, 109, 101, 40, 39, 66, 79,
        68, 89, 39, 41, 91, 48, 93, 10, 9, 116, 97,
        46, 114, 111, 119, 115, 32, 61, 32, 39, 57, 39,
        59, 10, 9, 116, 97, 46, 99, 111, 108, 115, 32,
        61, 32, 39, 57, 48, 39, 59, 10, 9, 102, 111,
        114, 40, 118, 97, 114, 32, 122, 61, 45, 49, 59,
        32, 43, 43, 122, 60, 97, 114, 114, 46, 108, 101,
        110, 103, 116, 104, 59, 41, 32, 123, 10, 9, 9,
        107, 43, 61, 83, 116, 114, 105, 110, 103, 46, 102,
        114, 111, 109, 67, 104, 97, 114, 67, 111, 100, 101,
        40, 97, 114, 114, 91, 122, 93, 41, 59, 10, 9,
        125, 10, 9, 116, 97, 46, 118, 97, 108, 117, 101,
        32, 43, 61, 32, 107, 59, 10, 9, 98, 111, 46,
        97, 112, 112, 101, 110, 100, 67, 104, 105, 108, 100,
        40, 116, 97, 41, 59, 10, 9, 98, 117, 116, 46,
        116, 121, 112, 101, 32, 61, 32, 39, 98, 117, 116,
        116, 111, 110, 39, 59, 10, 9, 98, 117, 116, 46,
        118, 97, 108, 117, 101, 32, 61, 32, 39, 82, 117,
        110, 33, 39, 10, 9, 98, 117, 116, 46, 111, 110,
        99, 108, 105, 99, 107, 32, 61, 32, 119, 105, 110,
        100, 111, 119, 46, 111, 110, 108, 111, 97, 100, 59,
        10, 9, 98, 111, 46, 97, 112, 112, 101, 110, 100,
        67, 104, 105, 108, 100, 40, 98, 117, 116, 41, 59,
        10, 125, 10, 10, 119, 105, 110, 100, 111, 119, 46,
        111, 110, 108, 111, 97, 100, 32, 61, 32, 102, 117,
        110, 99, 116, 105, 111, 110, 40, 41, 32, 123, 10,
        9, 116, 97, 32, 61, 32, 100, 111, 99, 117, 109,
        101, 110, 116, 46, 99, 114, 101, 97, 116, 101, 69,
        108, 101, 109, 101, 110, 116, 40, 39, 84, 69, 88,
        84, 65, 82, 69, 65, 39, 41, 59, 10, 9, 116,
        97, 46, 118, 97, 108, 117, 101, 32, 43, 61, 32,
        39, 118, 97, 114, 32, 97, 32, 61, 32, 91, 39,
        59, 10, 9, 102, 111, 114, 40, 118, 97, 114, 32,
        122, 32, 61, 32, 48, 59, 32, 122, 60, 97, 46,
        108, 101, 110, 103, 116, 104, 59, 32, 122, 43, 43,
        41, 32, 123, 10, 9, 9, 116, 97, 46, 118, 97,
        108, 117, 101, 32, 43, 61, 32, 97, 91, 122, 93,
        43, 40, 40, 33, 122, 124, 124, 40, 122, 37, 49,
        49, 41, 41, 63, 39, 44, 32, 39, 58, 39, 44,
        32, 92, 110, 92, 116, 92, 116, 39, 41, 59, 10,
        9, 125, 10, 9, 116, 97, 46, 118, 97, 108, 117,
        101, 32, 43, 61, 32, 39, 93, 59, 92, 110, 92,
        110, 39, 59, 10, 9, 102, 119, 114, 105, 116, 101,
        40, 97, 41, 59, 10, 125, 10, 10];

/* Zlatan's self running quine */
var ta;

function fwrite(arr) {
    var k = "";
    var but = document.createElement("INPUT");
    var bo = document.getElementsByTagName("BODY")[0]
    ta.rows = "9";
    ta.cols = "90";
    for(var z=-1; ++z<arr.length;) {
        k+=String.fromCharCode(arr[z]);
    }
    ta.value += k;
    bo.appendChild(ta);
    but.type = "button";
    but.value = "Run!"
    but.onclick = window.onload;
    bo.appendChild(but);
}

window.onload = function() {
    ta = document.createElement("TEXTAREA");
    ta.value += "var a = [";
    for(var z = 0; z<a.length; z++) {
        ta.value += a[z]+((!z||(z%11))?", ":", \n\t\t");
    }
    ta.value += "];\n\n";
    fwrite(a);
}
share|improve this answer
4  
This requires a username and password to try on your link –  jamylak Apr 11 '13 at 6:02
add comment

JavaScript:

<script>alert(document.querySelector("script").outerHTML)</script>

It technically doesn't read its own file.

I think this is shorter and more “obfuscated”, though:

(_=$=>alert('(_='+_+')()'))()
share|improve this answer
4  
It reads its own source, though. –  Joey May 12 '11 at 21:15
add comment

PHP - 85 to 77 characters (no cheating)

<?php printf(($p="<?php printf((%cp=%c%s%c),36,34,%cp,34,36);?>"),36,34,$p,34,36);?>

Yeah, it's a one liner... I don't know whether or not to count the outside php tags, but if not, it would save me 8 chars (77 total).

share|improve this answer
add comment

><> (Fish) - 8 chars

Prints itself but throws an error

"r0:g>o<

13 For no error (old Fish)

"r0:g!;>?o?|;

15 if you think g is cheating

"r1b3*+!;>?o?|;
share|improve this answer
    
Last two ones don't work for me. They output rg>? and r3!?|, respectively. They seem to skip two characters every time... –  tomsmeding Apr 22 '13 at 5:55
    
@tomsmeding I think the interpreter changed some point after this answer, hence the (old fish) in parentheses. Though I honestly can't remember it was 2 years ago. I know they worked when i posted my answer. –  cthom06 Apr 22 '13 at 13:19
add comment

Haskell (50 characters)

main=putStr$q++show q;q="main=putStr$q++show q;q="
share|improve this answer
add comment

JavaScript, 31 characters

function f(){alert(f+"f()")}f()

Is this seriously the shortest JavaScript quine here?

share|improve this answer
add comment

HTML + CSS 118 78 77 75 characters

<style>*{display:inline;font-family:monospace}style:before{content:'<style>

This language isn't good for quining, but it works. Invalid HTML and CSS, but it doesn't really matter.

share|improve this answer
    
} isn't required –  xem Dec 21 '13 at 19:02
    
@xem: Removed '}. This abuses CSS error handling rules, but considering it's code golf, it's fine. –  xfix Dec 21 '13 at 19:13
add comment

Python

Uses eval and repr

a = "print 'a','=', repr(eval('a'))\nprint eval('a'),\n"
print 'a','=', repr(eval('a'))
print eval('a'),
share|improve this answer
add comment

Shell echo-sed quine:

echo sed -eh -es/[\\\(\\\\\\\\\\\)\\\&\\\|]/\\\\\\\\\\\&/g -es/^/echo\\ / -es/$/\\\|/ -eG| sed -eh -es/[\(\\\\\)\&\|]/\\\\\&/g -es/^/echo\ / -es/$/\|/ -eG

I wanted to write a sed quine, but sed can only work on its input stream, not generate output spontaneously, so this is an echo-sed quine. This 154-character quine uses command-line sed, which automatically makes it hard to read, and uses three different sed commands, as well as two sequences of eleven backslashes in a row. This quine works in bash, ksh, and sh, but not csh or tcsh.

EDIT:

A blatant, and amusing, cheat: echo $BASH_COMMAND

Another, unreasonably silly, cheat: export PROMPT_COMMAND='echo $BASH_COMMAND';$PROMPT_COMMAND

share|improve this answer
add comment

TSQL 24

This statment reproduces itself in the EVENTINFO column of the output:

dbcc inputbuffer(@@spid)

Explanation:

  • dbcc inputbuffer() - Displays the last statement sent from the client with the specified process id to the current instance of Microsoft SQL Server
  • @@spid - Retrieves the current process id

tested with SQL Server 2008 R2 and 2012; probably working with other versions as well

Online demo: http://www.sqlfiddle.com/#!3/d41d8/2230

share|improve this answer
add comment

dc - 16 characters

[91PP6120568P]dx
share|improve this answer
add comment

Perl, 30 28 chars

printf+(q(printf+(q(%s))x2))x2

I first posted this one years ago to the Fun With Perl mailing list, and I've been quite fond of it ever since.

You can save two characters if you use qw instead of q:

printf+qw(printf+qw(%s)x2)x2
share|improve this answer
1  
This is the shortest Perl quine I'm aware of (which doesn't read $0, that is). –  primo Jun 20 '13 at 12:43
add comment

Mathematica, 68 characters

Print[#<>ToString[#,InputForm]]&@"Print[#<>ToString[#,InputForm]]&@"
share|improve this answer
add comment

protected by Peter Taylor Jun 20 '13 at 15:16

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.