Incorporating New Code Into a
Self-reproducing C Program


The following C program is also self-reproducing, that is, if compiled and run, the output is the same as the source. In this case, the program uses more sophisticated parts of C, but it is basically easier to understand. This program is slightly different from an earlier one because I was trying to figure out how to handle a '%' character inside double quotes. (In printing with printf, the % needs to be %%.) The execution below demonstrates that this program is self-reproducing. Here is the source: quine2.c.

Self reproducing program: quine2.c
#include<stdio.h>
char *a[] = {"\"};\n\n",
	"main() {\n",
	"  int i=0; char *b;\n",
	"  printf(\"#include<stdio.h>\\nchar *a[] = {\\\"\");\n",
	"  while(*a[i]) {\n",
	"    for(b=a[i];*b;b++)\n",
	"      switch(*b) {\n",
	"         case '\\n': printf(\"\\\\n\"); break;\n",
	"         case '\\\\': case '\\\"': putchar('\\\\'); \n",
	"         default: putchar(*b);\n",
	"      } \n",
	"    printf(\"\\\",\\n\\t\\\"\"); \n",
	"    i++;\n",
	"  }\n",
	"  i=0;\n",
	"  while(*a[i]) {for(b=a[i];*b;b++)putchar(*b);i++;}\n",
	"}\n",
	""};

main() {
  int i=0; char *b;
  printf("#include<stdio.h>\nchar *a[] = {\"");
  while(*a[i]) {
    for(b=a[i];*b;b++)
      switch(*b) {
         case '\n': printf("\\n"); break;
         case '\\': case '\"': putchar('\\'); 
         default: putchar(*b);
      } 
    printf("\",\n\t\""); 
    i++;
  }
  i=0;
  while(*a[i]) {for(b=a[i];*b;b++)putchar(*b);i++;}
}
% cc -o quine2 quine2.c
% quine2 > quine2_out.c
% diff quine2.c quine2_out.c
%


Next comes a simple program to open a file named "pifile" and to write the value of pi to that file. This program is a candidate for inclusion into the self-reproducing program.

Program to incorporate in: pi.c
#include <stdio.h>
#include <math.h>
int main() {
   FILE *outfile = fopen("pifile", "w");
   fprintf(outfile, "%20.16f\n",4.0*atan(1.0));
}
% rm pifile 
% cc -o pi pi.c -lm
% pi
% cat pifile
  3.1415926535897931


Finally, the following code shows the work of calculating pi and writing to a file incorporated into the self-reproducing program -- incorporated into the program in such a way that the resulting program is still self-reproducing. The new code is given below in red. The execution afterward shows that the new program does reproduce itself, and it does write out the value of pi. This example basically shows how to incorporate an arbitrary computation into a self-reproducing program, and it demonstrates that this can be done. Here is the source: quine2_pi.c.

Self reproducing program: quine_pi.c
#include<stdio.h>
#include<math.h>
char *a[] = {"\"};\n\n",
	"main() {\n",
	"  int i=0; char *b;\n",
	"  FILE *outfile = fopen(\"pifile\", \"w\");\n",
	"  fprintf(outfile, \"%20.16f\\n\",4.0*atan(1.0));\n",
	"  printf(\"#include<stdio.h>\\n#include<math.h>\\nchar *a[] = {\\\"\");\n",
	"  while(*a[i]) {\n",
	"    for(b=a[i];*b;b++)\n",
	"      switch(*b) {\n",
	"         case '\\n': printf(\"\\\\n\"); break;\n",
	"         case '\\\\': case '\\\"': putchar('\\\\'); \n",
	"         default: putchar(*b);\n",
	"      } \n",
	"    printf(\"\\\",\\n\\t\\\"\"); \n",
	"    i++;\n",
	"  }\n",
	"  i=0;\n",
	"  while(*a[i]) {for(b=a[i];*b;b++)putchar(*b);i++;}\n",
	"}\n",
	""};

main() {
  int i=0; char *b;
  FILE *outfile = fopen("pifile", "w");
  fprintf(outfile, "%20.16f\n",4.0*atan(1.0));
  printf("#include<stdio.h>\n#include<math.h>\nchar *a[] = {\"");
  while(*a[i]) {
    for(b=a[i];*b;b++)
      switch(*b) {
         case '\n': printf("\\n"); break;
         case '\\': case '\"': putchar('\\'); 
         default: putchar(*b);
      } 
    printf("\",\n\t\""); 
    i++;
  }
  i=0;
  while(*a[i]) {for(b=a[i];*b;b++)putchar(*b);i++;}
}
% rm pifile
% rm quine2_pi
% cc -o quine2_pi quine2_pi.c -lm
% quine2_pi > quine2_pi_out.c
% cat pifile
  3.1415926535897931
% diff quine2_pi.c quine2_pi_out.c
% 


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.