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.

link|improve this question
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
4  
@muntoo it's a play on "Learn you a Haskell for Great Good". – Rafe Kettler May 3 '11 at 2:52
1  
what about random text generator quines (a probability that the program will print itself) – SHiNKiROU May 14 '11 at 0:01
feedback

41 Answers

1 2

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

1
link|improve this answer
6  
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
8  
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 at 8:27
feedback

PostScript, 20 chars

Short and legit. 20 chars including trailing newline.

(dup == =)
dup == =
link|improve this answer
feedback

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.

link|improve this answer
feedback

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)+')'")
link|improve this answer
feedback

If we're going for short quines:

0 characters.

link|improve this answer
7  
Points for originality, but I'm adding this to the list of "ways to cheat". – Rafe Kettler Jan 28 '11 at 0:39
12  
@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
5  
Can we avoid up-voting silly answers? – marcog Jan 28 '11 at 11:26
6  
@marcog what is code golf but answering silly questions with rediculous (or rediculously complex) answers ;) – tobyodavies Jan 28 '11 at 12:01
4  
@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
feedback

Python (29)

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

from en.literateprograms.org

link|improve this answer
feedback

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...

link|improve this answer
3  
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
feedback

HQ9+ (1 character)

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

Q
link|improve this answer
3  
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
@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
feedback

Fob (135)

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

$$#<&$::#<&$:#<&#<&$:#<=#<&$&//%<//<.&%<<%.%<&>/////%<<%.<&.%<.%/////<&.%<<&/.%%<&>%</%<////<&.%<<%/<&.%%<&>/%//<&.%<</&.%%%<&>>/>>#<=
link|improve this answer
feedback

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);
        }
    }
}
link|improve this answer
feedback

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!
+[[>>+[>]+>+[<]<-]>>[>]<+<+++[<]<<+]>>+[>]+++[++++++++++>++[-<++++++++++++++++>]<.<-<]
link|improve this answer
1  
That's a clever way to do it. – Peter Olson Jun 22 '11 at 16:48
feedback

Javascript (36 char)

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

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

link|improve this answer
That... is impressive. You should explain how it works for me 8-| – TehShrike Sep 27 '11 at 19:18
1  
@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
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 at 18:55
feedback

JavaScript:

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

It technically doesn't read it's own file.

link|improve this answer
It reads its own source, though. – Joey May 12 '11 at 21:15
feedback

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.

link|improve this answer
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 at 16:13
feedback

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);
}
link|improve this answer
feedback

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

link|improve this answer
feedback

Python

Uses eval and repr

a = "print 'a','=', repr(eval('a'))\nprint eval('a'),\n"
print 'a','=', repr(eval('a'))
print eval('a'),
link|improve this answer
feedback

><> (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?|;
link|improve this answer
feedback

Haskell (50 characters)

main=putStr$q++show q;q="main=putStr$q++show q;q="
link|improve this answer
feedback

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

link|improve this answer
feedback

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).

link|improve this answer
feedback

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

link|improve this answer
feedback

C++ (350)

#include<iostream>
#include<fstream>
int main(){std::ofstream f;f.open("f.cpp");
#define B(x)x;f<<("B(" #x ")");
#define A(x)f<<("A(" #x ")");x;
B(f<<("#include<iostream>\n#include<fstream>\nint main(){std::ofstream f;f.open(\"f.cpp\");\n#define B(x)x;f<<(\"B(\" #x \")\");\n#define A(x)f<<(\"A(\" #x \")\");x;\n"))A(f<<("f.close();}\n"))f.close();}

Modified version of this.

Makes use of the C++ preprocessor.

link|improve this answer
feedback

Clojure - 1 char (cheating!) or 91 chars

The following works in Clojure:

1

However that's against the spirit of a quine! So how about:

((fn [x] (list x (list (quote quote) x))) (quote (fn [x] (list x (list (quote quote) x)))))
link|improve this answer
feedback

Groovy:

`s='s=\\\';s[0..1]+s[3]+s[0..1]+s[2]*6+s[3..-1]*2';s[0..1]+s[3]+s[0..1]+s[2]*6+s[3..-1]*`2

Edit

Works in GroovyConsole

link|improve this answer
feedback

Perl, 30 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.

link|improve this answer
feedback

Haskell, 93 68 characters

s="\nmain=putStrLn$\"s=\"++show s++s"
main=putStrLn$"s="++show s++s
link|improve this answer
feedback

Mathematica, 68 characters

Print[#<>ToString[#,InputForm]]&@"Print[#<>ToString[#,InputForm]]&@"
link|improve this answer
feedback

Bash, 67/51 chars

f () 
{ 
    printf "%s\n${!1} $1" "$(local -f ${!1})"
}
f FUNCNAME

And 51 chars:

trap -- 'printf "%s\n:" "$(trap -p DEBUG)"' DEBUG
:
link|improve this answer
feedback

C, 79 chars

Maybe the easiest one in C.

main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}

34 is the ASCII decimal for ".

link|improve this answer
feedback
1 2

Your Answer

 
or
required, but never shown

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