Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Quine (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== ===Constructive quines=== In general, the method used to create a quine in any programming language is to have, within the program, two pieces: (a) [[Source code|code]] used to do the actual printing and (b) [[data]] that represents the textual form of the code. The code functions by using the data to print the code (which makes sense since the data represents the textual form of the code), but it also uses the data, processed in a simple way, to print the textual representation of the data itself. Here are three small examples in Python3: <syntaxhighlight lang="python3"> # Example A. chr(39) == "'". a = 'a = {}{}{}; print(a.format(chr(39), a, chr(39)))'; print(a.format(chr(39), a, chr(39))) </syntaxhighlight> <syntaxhighlight lang="python3"> # Example B. chr(39) == "'". b = 'b = %s%s%s; print(b %% (chr(39), b, chr(39)))'; print(b % (chr(39), b, chr(39))) </syntaxhighlight> <syntaxhighlight lang="python3"> # Example C. %r will quote automatically. c = 'c = %r; print(c %% c)'; print(c % c) </syntaxhighlight>The following [[Java (programming language)|Java]] code demonstrates the basic structure of a quine. <syntaxhighlight lang="java"> public class Quine { public static void main(String[] args) { char q = 34; // Quotation mark character String[] l = { // Array of source code "public class Quine", "{", " public static void main(String[] args)", " {", " char q = 34; // Quotation mark character", " String[] l = { // Array of source code", " ", " };", " for (int i = 0; i < 6; i++) // Print opening code", " System.out.println(l[i]);", " for (int i = 0; i < l.length; i++) // Print string array", " System.out.println(l[6] + q + l[i] + q + ',');", " for (int i = 7; i < l.length; i++) // Print this code", " System.out.println(l[i]);", " }", "}", }; for (int i = 0; i < 6; i++) // Print opening code System.out.println(l[i]); for (int i = 0; i < l.length; i++) // Print string array System.out.println(l[6] + q + l[i] + q + ','); for (int i = 7; i < l.length; i++) // Print this code System.out.println(l[i]); } }</syntaxhighlight> The source code contains a string array of itself, which is output twice, once inside quotation marks. This code was adapted from an original post from c2.com, where the author, Jason Wilson, posted it as a minimalistic version of a Quine, without Java comments.<ref>{{cite web |url=http://wiki.c2.com/?QuineProgram |title=Quine Program |website=wiki.c2.com}}</ref> Thanks to new [https://openjdk.java.net/jeps/378 text blocks] feature in Java 15 (or newer), a more readable and simpler version is possible:<ref>{{Cite web|url=https://gist.github.com/destan/c0db5a237e9875a56141403aaa6cb9c7|title=Simple Java quine, self replicating (Self copying) Java code, with text blocks. This code can be run with Java 15+ or Java 13+ with special flags. License is public domain, no rights reserved}}</ref> <syntaxhighlight lang="java"> public class Quine { public static void main(String[] args) { String textBlockQuotes = new String(new char[]{'"', '"', '"'}); char newLine = 10; String source = """ public class Quine { public static void main(String[] args) { String textBlockQuotes = new String(new char[]{'"', '"', '"'}); char newLine = 10; String source = %s; System.out.print(source.formatted(textBlockQuotes + newLine + source + textBlockQuotes)); } } """; System.out.print(source.formatted(textBlockQuotes + newLine + source + textBlockQuotes)); } } </syntaxhighlight>The same idea is used in the following [[SQL]] quine:<syntaxhighlight lang="sql"> SELECT REPLACE(REPLACE('SELECT REPLACE(REPLACE("$",CHAR(34),CHAR(39)),CHAR(36),"$") AS Quine',CHAR(34),CHAR(39)),CHAR(36),'SELECT REPLACE(REPLACE("$",CHAR(34),CHAR(39)),CHAR(36),"$") AS Quine') AS Quine </syntaxhighlight> ===Eval quines=== Some programming languages have the ability to evaluate a string as a program. Quines can take advantage of this feature. For example, this [[Ruby (programming language)|Ruby]] quine: <syntaxhighlight lang="ruby">eval s="print 'eval s=';p s"</syntaxhighlight> [[Lua (programming language)|Lua]] can do: <syntaxhighlight lang="lua">s="print(string.format('s=%c%s%c; load(s)()',34,s,34))"; load(s)()</syntaxhighlight> In Python 3.8: <syntaxhighlight lang="python3"> exec(s:='print("exec(s:=%r)"%s)') </syntaxhighlight> ==="Cheating" quines=== ==== Self-evaluation ==== In many functional languages, including [[Scheme (programming language)|Scheme]] and other [[Lisp (programming language)|Lisps]], and interactive languages such as [[APL (programming language)|APL]], numbers are self-evaluating. In [[TI-BASIC]], if the last line of a program returns a value, the returned value is displayed on the screen. Therefore, in such languages a program consisting of only a single digit results in a 1-byte quine. Since such code does not ''construct'' itself, this is often considered cheating. <syntaxhighlight lang="basic"> 1 </syntaxhighlight> ==== Empty quines ==== In some languages, particularly [[scripting language]]s but also [[C (programming language)|C]], an empty source file is a fixed point of the language, being a valid program that produces no output.{{efn|1=Examples include [[Bash (Unix shell)|Bash]], [[Perl]], and [[Python (programming language)|Python]]}} Such an empty program, submitted as "the world's smallest self reproducing program", once won the "worst abuse of the rules" prize in the [[International Obfuscated C Code Contest]].<ref>{{cite web |url = http://www0.us.ioccc.org/1994/smr.hint |title = IOCCC 1994 Worst Abuse of the Rules |archive-url=https://web.archive.org/web/20201112015540/http://www0.us.ioccc.org/1994/smr.hint |archive-date=12 November 2020 |url-status=dead}}</ref> The program was not actually compiled, but used <code>cp</code> to copy the file into another file, which could be executed to print nothing.<ref>{{cite web |title=Makefile |url=http://www0.us.ioccc.org/1994/Makefile |website=IOCCC.org |access-date=4 April 2019 |archive-date=23 April 2019 |archive-url=https://web.archive.org/web/20190423002150/http://www0.us.ioccc.org/1994/Makefile |url-status=dead }}</ref> ==== Source code inspection ==== Quines, per definition, cannot receive ''any'' form of input, including reading a file, which means a quine is considered to be "cheating" if it looks at its own source code. The following [[Unix shell|shell]] script is not a quine: <syntaxhighlight lang="bash"> #!/bin/sh # Invalid quine. # Reading the executed file from disk is cheating. cat $0 </syntaxhighlight> A shorter variant, exploiting the behaviour of [[Shebang (Unix)|shebang]] directives: <syntaxhighlight lang="bash"> #!/bin/cat </syntaxhighlight> Other questionable techniques include making use of compiler messages; for example, in the [[GW-BASIC]] environment, entering "Syntax Error" will cause the interpreter to respond with "Syntax Error". Quine code can also be outputted visually, for example it's used to visualize the neutral zone in [[Yars' Revenge]], along with [[Syntactic_sugar#Syntactic_saccharin|syntactic saccharin]], to obfuscate the source code.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)