# Golf you a quine for great good!

Using your language of choice, golf a quine.

A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

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.

No error quines -- there is already a separate challenge for error quines.

Points for:

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

The following Stack Snippet can be used to get a quick view of the current score in each language, and thus to know which languages have existing answers and what sort of target you have to beat:

var QUESTION_ID=69;
var OVERRIDE_USER=98;

getAnswers();var SCORE_REG=(function(){var headerTag=String.raw h\d
var score=String.raw \-?\d+\.?\d*
var normalText=String.raw [^\n<>]*
var strikethrough=String.raw <s>${normalText}</s>|<strike>${normalText}</strike>|<del>${normalText}</del> var noDigitText=String.raw [^\n\d<>]* var htmlTag=String.raw <[^\n<>]+> return new RegExp(String.raw <${headerTag}>+String.raw \s*([^\n,]*[^\s,]),.*?+String.raw (${score})+String.raw (?=+String.raw ${noDigitText}+String.raw (?:(?:${strikethrough}|${htmlTag})${noDigitText})*+String.raw </${headerTag}>+String.raw ))})();var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}
body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)
valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,})});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)
if(languages.hasOwnProperty(lang))
langs.push(languages[lang]);langs.sort(function(a,b){if(a.uniq>b.uniq)return 1;if(a.uniq<b.uniq)return-1;return 0});for(var i=0;i<langs.length;++i)
{var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

• Do you not mean, "Golf you a quine for greater good!"? – Mateen Ulhaq May 3 '11 at 2:49
• @muntoo it's a play on "Learn you a Haskell for Great Good". – Rafe Kettler May 3 '11 at 2:52

## C, 125 84 chars

main(){char*p="main(){char*p=%c%s%c,c='%c',s[256];sprintf(s,p,c,p,c,c);puts(s);}",c='"',s[256];sprintf(s,p,c,p,c,c);puts(s);}


It turns out that my idea was implemented much better:

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

• You could shave 9 chars off the shorter version by leaving out the trailing newline. – Ilmari Karonen Feb 3 '12 at 18:55

# F♯# - 349 Characters

let s="\\\"\nnlet s=let z a b=s.Substring(a,b)System.Console.WriteLine()z 4 6+z 1 1+z 0 1+z 0 1+z 0 1+z 1 1+z 0 1+z 3 1+z 3 1+z 4 169+z 1 1+z 2 1+z 10 26+z 2 1+z 36 25+z 62 111+z 61 1"
let z a b=s.Substring(a,b)
System.Console.WriteLine(z 4 6+z 1 1+z 0 1+z 0 1+z 0 1+z 1 1+z 0 1+z 3 1+z 3 1+z 4 169+z 1 1+z 2 1+z 10 26+z 2 1+z 36 25+z 62 111+z 61 1)


My first attempt at a quine - probably an easier (or shorter) way to do it, but not a bad first attempt I don't think

set c {set c {$c};puts [subst -noc \$c]};puts [subst -noc $c]  ## Erlang escript 225164 140 $ escript quine

main(_)->[A|B]=["main(_)->[A|B]=[","],io:put_chars([10,A,34,A,34,44,34,B,34,B,10,10])."],io:put_chars([10,A,34,A,34,44,34,B,34,B,10,10]).

$ Apparently escript has to have at least three lines. # Go - 583 Just because d; package main import "fmt" func main(){ a := string(byte(34)) b := []string{ "package main", "import fmt", "func main(){", " a := string(byte(34))", " b := []string{", " ", " }", " for i:=0;i<5;i++{if i != 1{fmt.Println(b[i])}else{fmt.Println(b[i][:7]+a+b[i][7:]+a)}}", " for _,v:=range b{fmt.Println(b[6]+a+v+a+string(','))}", " for i:=7;i<9;i++{fmt.Println(b[i])}", "}", } for i:=0;i<5;i++{if i != 1{fmt.Println(b[i])}else{fmt.Println(b[i][:7]+a+b[i][7:]+a)}} for _,v:=range b{fmt.Println(b[5]+a+v+a+string(','))} for i:=7;i<11;i++{fmt.Println(b[i])} }  • this is awesome. – cat Dec 7 '15 at 14:50 # Cobra - 143 class P def main s='class P{2} def main{2} s={1}{0}{1}{2} Console.write(s,s,39to char,10to char)' Console.write(s,s,39to char,10to char)  ## Lua, 76 characters s="s=%c%s%c;print(string.format(s,34,s,34))";print(string.format(s,34,s,34))  Another one with the usual format string technique. ## Julia, 101 characters s="s=%c%s%c;@printf %c%s%c 34 s 34 34 s 34";@printf "s=%c%s%c;@printf %c%s%c 34 s 34" 34 s 34 34 s 34  It's the usual format string technique, but unfortunately you can't get the format specification string from a variable in Julia, so I have to include it twice in the code, which blows everything up. ## Minkolang 0.9, 10 bytes This language was made after this challenge, but was not made for it. "66*2-(O).  Like other 2D languages, the " makes everything between it and the next " a string. 66*2- adds the not-included " and (O). prints everything out and stops. ## Scala, 84 bytes val d=""" print("val d=\"\"\""+d+"\"\"\""+d) """ print("val d=\"\"\""+d+"\"\"\""+d)  Kinda straightforward, but putting it out there for completion. ## Seriously, 2 bytes 1  Pushes the number 1, implicit print with trailing newline. Since this is fairly trivial, here is the smallest non-trivial quine which can contain arbitrary characters (12 bytes): è";ƒ"@+;ƒ  ## Python 3 - 58 Characters Since there is a Python 2 version, I suppose this is acceptable: x='x={};print(x.format(repr(x)))';print(x.format(repr(x)))  ## Java, 190 Characters class I{public static void main(String[]a){String s="class I{public static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s));}}";System.out.print(s.format(s,34,s));}}

• Since ye olde Java SE 8, you can put static methods (such as main) in interfaces - no public. Also print and format methods can be collapsed into printf. – Tom Hawtin - tackline Oct 6 '18 at 23:08

# Reng v.1.3, 7 bytes

Try it out here!

"rYao;~


" begins a quote string, and reads all of those characters. r reverses the stack, and Y pushes the char code of ". a begins a one-way mirror loop, o outputs the character, and ; mirrors while the stack is truthy. After the zero is met, we advance to ~ and the program ends.

# Reng v.2, 8 bytes

{n6G*o}


This one is a little more interesting. {...} is a code block, and  executes a code block. n outputs the codeblock already on the stack, 6G* makes a  character (6*16 = 96), and o output's that.

# Java 2146 2118 bytes

A legitimate attempt at a quine. This was automagically generated. I could have robbed someone elses quining technique but decided against it.

interface q{static void main(String[] args){char[] s={32,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,112,117,98,108,105,99,32,99,108,97,115,115,32,81,117,105,110,101,123,112,117,98,108,105,99,32,115,116,97,116,105,99,32,118,111,105,100,32,109,97,105,110,40,83,116,114,105,110,103,91,93,32,97,114,103,115,41,123,34,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,99,104,97,114,91,93,32,115,61,123,34,41,59,102,111,114,40,105,110,116,32,105,61,48,59,105,60,115,46,108,101,110,103,116,104,45,49,59,105,43,43,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,105,93,43,34,44,34,41,59,125,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,115,46,108,101,110,103,116,104,45,49,93,43,34,125,59,34,41,59,102,111,114,40,99,104,97,114,32,99,58,32,115,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,99,41,59,125,125,125,32,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,112,117,98,108,105,99,32,99,108,97,115,115,32,81,117,105,110,101,123,112,117,98,108,105,99,32,115,116,97,116,105,99,32,118,111,105,100,32,109,97,105,110,40,83,116,114,105,110,103,91,93,32,97,114,103,115,41,123,34,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,99,104,97,114,91,93,32,115,61,123,34,41,59,102,111,114,40,105,110,116,32,105,61,48,59,105,60,115,46,108,101,110,103,116,104,45,49,59,105,43,43,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,105,93,43,34,44,34,41,59,125,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,115,91,115,46,108,101,110,103,116,104,45,49,93,43,34,125,59,34,41,59,102,111,114,40,99,104,97,114,32,99,58,32,115,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,99,41,59,125,125,125};System.out.print("interface q{static void main(String[] args){");System.out.print("char[] s={");for(int i=0;i<s.length-1;i++){System.out.print((int)s[i]+",");}System.out.print((int)s[s.length-1]+"};");for(char c: s){System.out.print(c);}}}


# Julia, 36 bytes

(~=:@printf "(~=:%s)|>eval" ~)|>eval


Try it online!

### Background

Unlike many other languages, Julia's eval doesn't work as expected with a string; for example, eval("print(42)") just returns the string print(42).

To actually executed print(42) with eval;, we have to pass an Expr to eval. This can be done by invoking parse on a string (e.g., eval(parse("print(42)"))) or by passing an Expr literal to eval (e.g., eval(:(print(42))).

Now, while : is a unary operator and :print works fine on its own, :print(42) does not, as it is parsed as (:print)(42), making all parentheses in :(print(42)) mandatory.

However, if we use the macro @printf instead, the parsing rules change, and :@printf(42) works as intended. Also, macro calls also do not require parentheses, and :@printf 42 saves one byte over :(print(42)).

### How it works

:@printf "(~=:%s)|>eval" ~ constructs the Expr that calls @printf with the specified format string and additional argument ~. Here, ~ is simply a variable reference; the name is arbitrary.

~=<Expr> saves the generated Expr in the variable ~, which will be accessible when the Expr is evaluated.

Finally, (<assigment>)|>eval calls eval with the return value of the assignment, i.e., the Expr that was assigned to ~.

# Common Lisp - 73 35

Thanks to reader variables written #n= and #n#, with n an integer, Lisp code can be self-referential. Also, the printing functions can emit such reader variables when told to handle circular structures. The WRITE function accepts a :circle parameter for that purpose. It also returns the value being printed, which means that we have to globally set *PRINT-CIRCLE* to T (the initial, standard value is NIL), otherwise the REPL would report a stack-overflow exception when printing that value. Initializing the variable takes a lot of bytes and so the shorter solution is to return another value:

#1=(PROGN (WRITE '#1# :CIRCLE T) T)


# Scala, 56 bytes

val s="val s=%c%s%c;printf(s,34,s,34)";printf(s,34,s,34)


First defines s as val s=%c%s%c;printf(s,34,s,34), then prints it formatted with double quotes (ascii 34) and itself.

# Dart, 185 bytes

main(){var c=new String.fromCharCode(34);var l=["main(){var c=new String.fromCharCode(34);var l=[","];print(l[0]+c+l[0]+c+','+c+l[1]+c+l[1]);}"];print(l[0]+c+l[0]+c+','+c+l[1]+c+l[1]);}


# Dip, 10 bytes

"N+|+"N+|+


Explanation:

"N+|+"      Push string
N+    Add " on the left side of the string
|+  Duplicate and concatenate


# Zetaplex, 13 bytes

"Sr34SZOsDrFe


Zetaplex is a variant of gammaplex. In it, commands are pairs of chars and act on an infinite stack.

# stacked, 16 bytes

Try it here!

[put ':!' put]:!


[put ':!' put] is a func, which is pushed to the stack. : duplicates it, and ! executes it, which prints the function, then :!.

# D, 85 bytes

enum c=q{import std.stdio;void main(){write("enum c=q{"~c~"};mixin(c);");}};mixin(c);


# Racket, 90 bytes

((lambda (x) (display (list (~a x) (~v x)))) '(lambda (x) (display (list (~a x) (~v x)))))

• You can remove unnecessary spaces to golf bytes. I'm not sure what spaces you can remove (cause I use Clojure), but there's definitely spaces that you can remove. – clismique Jan 23 '17 at 9:37
• Not in this case, because the input has to match the printed output – Matthew Butterick Jan 23 '17 at 13:12
• Oh... yeah, whoops. I'm just looking at the spaces and thinking that they can be golfed somehow. Well, TIL. – clismique Jan 23 '17 at 22:00

# Pip, 15 bytes

V Y"V Y.RPy"


The code includes a trailing newline. Try it online!

### Explanation

  Y"..."  Yank this string into the y variable
V         and evaluate it


which executes this:

V Y      A Pattern literal (for our purposes, a string-like object without "")
.     to which we concatenate
RPy  repr(y), which wraps y in ""


The result is autoprinted with a trailing newline.

### Bonus submission, 18 bytes

The above is the shortest Pip quine that I've found, but unfortunately it doesn't do so well when you try to modify it for other quine challenges. I have found this one to be more payload-friendly:

Y"Y yRsRPy"yRsRPy

Y"Y yRsRPy"        Yank that string into y
yRsRPy  Take y and replace space with repr(y)


## AWK, 72 bytes

BEGIN{s="BEGIN{s=%c%s%c;printf s,34,s,34}";printf s,34,s,34}


In 6 years nobody had written an AWK solution :(

This can be run from command-line as:

awk 'BEGIN{s="BEGIN{s=%c%s%c;printf s,34,s,34}";printf s,34,s,34}'


or placed in a file, FILE and run as:

awk -f FILE


Note: no newline is printed so if storing in a file... the file shouldn't have an EOF in it... maybe? We can add an EOF by adding print statements but that adds 12 bytes. This happens to be my first ever quine. :) It took me a bit to figure out how to get the quotation marks, since I first wanted to use \" but I'd have to escape the \ and then escape that one... ASCII to the rescue :)

• After I posted this I was curious if there was an extant AWK quine example. Apparently so at nyx.net/~gthompso/self_awk.txt. Given that, I believe this might be the shortest possible AWK quine. – Robert Benson Feb 23 '17 at 21:07

# k, 48 bytes

This is, of course, ignoring the trivial quines, such as () or 1.

{}0:(c$34)/{(x;x;())}"{}0:(c$34)/{(x;x;())}"


Try it out.

# Scala, 540 Bytes

object Q {
def main(a:Array[String]):Unit={
val d=Seq(
"object Q {",
"def main(a:Array[String]):Unit={",
"val d=Seq(",
"val c=println(_:String)",
"val b=(x:Int)=>x.toChar.toString",
"d take 3 map c",
"val a=d.zipWithIndex.map(x=>b(34)+x._1+b(34)+(if(x._2==d.size-1)b(9).drop(1)else b(44)))",
"a.map(c)",
"c(b(41))",
"d.drop(3).map(c)",
"c(b(125))",
"c(b(125))"
)
val c=println(_:String)
val b=(x:Int)=>x.toChar.toString
d take 3 map c
val a=d.zipWithIndex.map(x=>b(34)+x._1+b(34)+(if(x._2==d.size-1)b(9).drop(1)else b(44)))
a.map(c)
c(b(41))
d.drop(3).map(c)
c(b(125))
c(b(125))
}
}


Can probably be improved. I decided to call it quits for now as it is 5 in the morning. I think something that can be changed to make this smaller is to encode the d value in some other way other than a sequence of strings. I haven't thought of a cool way to do it yet, though. This would also add to the obfuscation part of the challenge.

## PowerShell, 24 bytes, 21 characters

.($s={".($s={$s})"})  I'm going to attempt to explain this, but be forewarned that I'm terrible at explaining myself. This code sets$s to the following:

".($s={$s})"


This recursively sets the variable $s (the first$s is a plain string, but the second $s is the variable$s) in itself and then the block inside parentheses echoes $s, which at time of execution will be the following: .($s={".($s={$s})"})


# Hack 60 bytes

<?hh printf($p='<?hh printf($p=%c%s%c,39,$p,39);',39,$p,39);
`

Only language I could think to do it in that I didn't see on the list.