Quine

The phenomena of self-replicating codes in various programming languages

Author's avatar
Publisher's avatar

Tungsten W.

Published in UX Collective · 3 min read · Oct 31, 2021

  • c
  • cpp
  • python

A quine is referred to the code that upon execution, will produce the exact same code that was executed prior as the output. It exhibits the idempotency property. Quine is also known as "self-replicating programs" or "self-copying programs."

C

const unsigned char data[] = { /* snip */ };
#include <stdio.h>
int main() {
  printf( "const unsigned char data[] = {" );
  for ( int i=0; i<sizeof(data); i++ ) {
      printf( " %0#4x,", data[i] );
  }
  printf( "\n};\n\n" );
  for ( int j=0; j<sizeof(data); j++)
      putchar( data[j] );
  return 0;
}
int main(void) {
  char str[]= " int main(void)\
{ char str[]= %c%s%c;\
printf(str, 0x22, str, 0x22);}";
  printf(str, 0x22, str, 0x22);}
main(){char*s="main(){char*s=%c%s%c;printf(s,34,s,34);}";printf(s,34,s,34);}

CoffeeScript

s="s=%j;console.log s,s";console.log s,s

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]);
  }
}

JavaScript

s = "s=%j;console.log(s,s)";
console.log(
  s,
  s
)(function a() {
  console.log("(" + a + ")");
})();

code = 'var q=unescape("%27");console.log("code="+q+code+q+";eval(code)")';
eval(code);

Lua

s="s=%qprint(s:format(s))"print(s:format(s))

Perl

$s='$s=%c%s%c;printf($s,39,$s,39);';printf($s,39,$s,39);
$s=q($s=q(%s);printf($s,%s););printf($s,%s);

Python

_='_=%r;print (_%%_)';print (_%_)
s='s=%r;print(s%%s)';print(s%s)
s='s=%r;print(s%%s,sep="")';print(s%s,sep="")

Ruby

s="s=%p;printf s,s";printf s,s
s="s=%p;print s%%s";print s%s
s="s=%p;puts s%%s";puts s%s
s="s=%c%s%c;printf s,34,s,34";printf s,34,s,34

Shell

s='s=\47%s\47;printf "$s" "$s"';printf "$s" "$s"

Scheme

(define x '(
  (display "(define x '(")
  (newline)
  (map (lambda (s) (write s) (newline)) x)
  (display "))")
  (newline)
  (display "(map eval x)")
  (newline)
  ))
(map eval x)

References

::apa-reference

authors:


::

::apa-reference

authors:


::

  • c
  • cpp
  • python

Explore Articles