Portable Self-Replicating C Contest

Geoff Clemm geoff at tom.harvard.edu
Sun Mar 26 04:05:00 AEST 1989


Here is an old self-replicating program I did a while back ... no attempt
was made to make the program short.  It may not be ansi but it should be
trivial to make it so.

------------  cut here -----------------
char *text =
"\n\
#include <stdio.h>\n\
main()\n\
{\n\
   (void)printf(\"char *text =\\n\\\"\");\n\
   out(text);\n\
   (void)printf(\"\\\";\\n%s\\n\", text); exit(0);};\n\
\n\
out(s)\n\
   char *s;\n\
{\n\
   while (*s != 0) {\n\
      if (*s == '\\n') (void)printf(\"\\\\n\\\\\");\n\
      if (*s == '\\\\' || *s == '\"') (void)putchar('\\\\');\n\
      (void)putchar(*s); s += 1; };};\n\
";

#include <stdio.h>
main()
{
   (void)printf("char *text =\n\"");
   out(text);
   (void)printf("\";\n%s\n", text); exit(0);};

out(s)
   char *s;
{
   while (*s != 0) {
      if (*s == '\n') (void)printf("\\n\\");
      if (*s == '\\' || *s == '"') (void)putchar('\\');
      (void)putchar(*s); s += 1; };};

------------  cut here -----------------

Geoff Clemm
geoff at harvard.harvard.edu



More information about the Comp.std.c mailing list