Selfish Code

Selfish Code

The following collection of self-reproducing code in various programming languages was collected by Joe Miller and myself. We started from some standard examples in C and tried to generalize to other languages.

Near the bottom, you can find code that is simultaneously selfish in more than one language! So far Joe has dual selfish C-tcl and C-lisp code. Anyone care to give it a try? Particularly challenging would be, say, Fortran-TeX or, even worse, three languages.

I haven't updated this page in a long time. For a much larger collection of selfish code, you should check out Gary Thompson's Quine Page, which contains many of the programs listed here, among others.

Chris Hruska, chruska at math.uchicago.edu

The following is a collection of ``selfish'' programs. Selfish programs are self-reproducing programs; programs that produce their own source code. Because of the diversity of languages I don't have a strict definition of selfish. In a functional language a selfish program usually evaluates to itself. In an imperative language a selfish program usually prints itself out. There is no reason to make these hard and fast rules though. I included anything that was short, clever, or interesting. There are a huge number of languages and almost all of them admit selfish programs. This is by no means a canonical list, but perhaps someday it will be.

Joe Miller, millerj7 at indiana.edu

Language: C

Author: Unknown
Notes: The first three examples are variations of the standard ``selfish'' C program. These examples were the primary inspiration for the rest of this silly collection.

main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,3
4,c,34);}
Author: Unknown
Notes: Modified by Joe. This causes some warnings but should compile fine.

p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Author: Thad Smith
Notes: The author claims that this one is strict ANSI. Looks good to me.

#include<stdio.h>
main(){char*c="\"#include<stdio.h>%cmain(){char*c=%c%c%c%.102s%cn%c
;printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);} ";printf(c+2,c[10
2],c[1],*c,*c,c,*c,c[1]);exit(0);}
Author: Joe Miller

#define T(a) main(){printf(a,#a);}
T("#define T(a) main(){printf(a,#a);} T(%s)")
Author: Dan Hoey
Notes: Modified slightly by Joe. This is not only self-printing, it's a palindrome.

/**/char q='"',*a="*//**/char q='%c',*a=%c%s%c*/};)b(stup;]d[b=]d-852
[b)--d(elihw;)q,a,q,q,2+a,b(ftnirps{)(niam;031=d tni;]952[b,",b[259];
int d=130;main(){sprintf(b,a+2,q,q,a,q);while(d--)b[258-d]=b[d];puts(
b);}/*c%s%c%=a*,'c%'=q rahc/**//*"=a*,'"'=q rahc/**/
Author: smr
Notes: This program won the "Worst Abuse of the Rules" award in the 1994 Obfuscated C Code contest. It claims to be the world's shortest self-producing C program. Hey, it's also a palindrome! Some compilers give fatal errors on this one.



Pretty short, eh?


Language: Java

Author: Dario Dariol
Notes: Wow, that's long! Is this really the shortest selfish Java program out there? (It is supposed to be all one line.) Java seems to be a pretty wordy language.

import java.text.*;class a{public static void main(String x[]){char b[]={34};
char c[]={123};String s[]=new String[3];s[0]="import java.text.*;class a{2}
public static void main(String x[]){2}char b[]={2}34};char c[]={2}123};
String s[]=new String[3];s[0]={1}{0}{1};s[1]=new String(b);s[2]=new String(c);
System.out.println(MessageFormat.format(s[0],s));}}";s[1]=new String(b);s[2]=
new String(c);System.out.println(MessageFormat.format(s[0],s));}}

Language: Pascal

Author: Oliver Heen
Notes: This program works in Borland Turbo-Pascal (not std Pascal or ucsb).

{ One single line }
CONST T=';BEGIN WRITE(#67#79#78#83#84#32#84
#61#39,T,#39,T)END.';BEGIN WRITE(#67#79#78#83
#84#32#84#61#39,T,#39,T)END.
Author: Oliver Heen
Notes: This program is described by the author as "Self Recognizing". The output is true if and only if the input is the same as the source code! Again this program is written for Turbo Pascal.

CONST A=';VAR S,T:STRING;BEGIN READLN(S);READLN(T); WRITELN(S+T=#67#79#78#83#84#32#65#61#39+A+#39+A)END.'
;VAR S,T:STRING;BEGIN READLN(S);READLN(T);WRITELN(S+T=#67#79#78#83#84#32#65#61#39+A+#39+A)END.

Language: Tcl

Author: Joe Miller
Notes: This has the unfortunate side effect of putting itself in x.

set x {set x {@}; regsub @   x; set x}; regsub @   x; set x
Author: Joe Miller

join [split a{a} a] {join [split a{a} a] }
Author: Joe Miller

join {{} { }} {join {{} { }} }

Language: LISP

Author: Chris Hruska
Notes: There are several silly examples like this.

T
Author: Chris Hruska

((LAMBDA (X) (LIST X (LIST 'QUOTE X))) '(LAMBDA (X) (LIST X (LIST 'QU
OTE X))))
Author: Joe Miller
Notes: In Common Lisp this should actually evaluate to the above list. In some dialects, however, it evaluates to itself.

((LAMBDA (X) `(,X ',X)) '(LAMBDA (X) `(,X ',X)))

Language: Fortran 77

Author: Myron A. Calhoun

      character*999s,p 
datas/'''/end(6x,a17/3(5x,a1,a66/),6x,a40/6x,a66/6x,a3)character*
'999s,p datas/p=s(64:70)//s(:1)//s(:1)//s(:176)//s(:2)write(*,s(6:5
'6))s(48:),p,p,s,p(67:),s,p(133:),s(71:),s(111:),s(3:)'/
p=s(64:70)//s(:1)//s(:1)//s(:176)//s(:2)
write(*,s(6:56))s(48:),p,p,s,p(67:),s,p(133:),s(71:),s(111:),s(3:)
end

Language: BASIC

Author: Chris Hruska
Notes: This example should technically be forbidden since it gets the source code from an external source, but it's so short that I had to include it.

10 LIST
Author: Chris Hruska
Notes: This is not only a bit of a cheat, its palindromic nature is also a cheat.

10 LIST : REM MER : TSIL 01
Author: Chris Hruska

a60chr33 3334):d60chr33 3358)+chr33 3399)+chr33 3336)+chr33 3361):b60"a60chr33 3334):d33br>=chr33 3358)+chr33 3399)+chr33 3336)+chr33 3361):b60":c60":?b33br>a :?b33br>
Author: Joseph Miller

a01]="a0":a02]="]=":a03]="q60chr33 3334):for i=1 to 3:?a01]str33 33i)a
02]q0i]q33 3358);:next i:?a03]":q60chr33 3334):for i=1 to 3:?a0
1]str33 33i)a02]q0i]q33 3358);:next i:?a03]
Author: Joseph Miller

a60"?chr33 3397)chr33 3336)chr33 3361)chr33 3334)a33 3334)chr33 3358)a :?chr33 3397)
chr33 3336)chr33 3361)chr33 3334)a33 3334)chr33 3358)a33br>

Language: AWK

Author: Chris Hruska
Notes: This one is little more than the standard selfish C program with main replaced by BEGIN.

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

Language: MUMPS (now called just M)

Author: Michael Turniansky
Notes: I haven't been able to test this one because I don't have access to an M compiler, but the author assures me that it works.

S Y=(34),A="W ""S Y=(34),A=""_Y X ""F %=1:1:6 W (A,Y,%),Y,Y"" W
Y,"" X A""" X A

Language: Perl

Author: Jeff Pinyan
Notes: The following Perl script looks like a cheat to me, but I don't know enough Perl to be sure.

#!/usr/bin/perl

seek DATA, 0, 0;
print ;

__DATA__
Author: Christoph Durr
Notes: Looks like another rewrite of the standard selfish C program to me.

='=%c%s%c;printf,39,,39;';printf,39,,39;

Language: TeX

Author: Denis Hirschfeldt
Notes: This first one is a LaTeX2e document which produces a printout of its own source code. The first character is a space. But shouldn't a printout of source code be in a fixed width font?

 documentclass{book}pagestyle
{empty}def{nsuremath{ackslash}}def
{def{}def;{}defd{}}def
{\} oindentg def gdef{ b
}def; c }defd{ d}def{ e
\}g} begin{document} f end
{document}}defg{  documentclass`bookd pagestyle
`emptyd def a` ensuremath` backslashdd def f
` def b` ad def c` `d def d` dd def
 e`  d noindent g b def b g c def b` a b
d def c` a c d def d` a dd def e` a e
  d g d a begin `document d a f a end
 `document dd}egin{document} nd{document}
Author: Joe Miller
Notes: The following TeX document produces a printout of its own source code. The printout is in cmtt10. The reference to character codes is machine independent since it refers to the positions of characters in a standard TeX font. On behalf of the purists (i.e. Chris), Joe had it omit the page number from the printout.

obeylines
opagenumbers	tdef[#1]#2[#3[#4
#5{#1obeylines#1nopagenumbers#1tt#1def#1[#21]#22[#23[#24
#25#3#5#4#1[#1char92]#1#2[#1char123[#1char125
#3#1[#1#21]#1#22[#1#23[#1#24
#3#1#25#4#4#1bye}[ar92]#[ar123[ar125
{[#1]#2[#3[#4
{#5}}ye

Multiple Languages:

The following code is selfish in more than one language! That's right. If you run it in more than one language, it produces itself as output.

Author: Joe Miller
Languages: C and tcl

#define set char*f=
#define F
#define proc main(){
set F "#define set char*f=
#define F
#define proc main(){
set F %c%s%c;
proc /* {} {}
puts -nonewline %cformat %cF 34 %cF 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
";
proc /* {} {}
puts -nonewline [format 34 34 91 36 36]
set a {*/printf(f,34,f,34,91,36,36);}
Author: Joe Miller
Languages: C and Lisp
Notes: The Lisp sucks. It consists almost entirely of destructive variable assignments and sequences of statements. Oh well. For reasons I do not want to discuss, the first line is blank.

t(setq /*;*/){}main(){char q='"',s='\';char*a=
"~%t(setq /*;*/){}main(){char q='~A';char*a=
~S;char*b=/*
)(setq a ~S)
(setq */ ~S;printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}~%";char*b=/*
)(setq a "\"',s='\\")
(setq */ "
t(setq /*;*/){}main(){char q='%c%c',s='%c%c';char*a=
%c%s%c;char*b=/*
)(setq a %c%c%c%c%c',s='%c%c%c%c%c)
(setq */ %c%s%c;printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}
";printf(b,s,q,s,s,q,a,q,q,s,s,s,q,s,s,s,s,q,q,b,q/*
)(format t /* a /* a */);}

chruska at math.uchicago.edu
18 November 1999