Search code examples
algorithmlanguage-agnosticquine

Techniques to implement quines


A few years back I was thinking about ways to make a program that prints its own source code and I came up with these two approaches:

  • the program prints the .c or .cpp file based upon the executable name (app1 prints app1.cpp). but this will fail in case the .cpp file is located somewhere else.
  • the program makes "clever" use of strings (I forgot the source code of this), and prints itself.

Is there any other algorithm a program can use to print its own source code?


Solution

  • I think your two cases cover all the options. Case (1) covers cases of the form "load the program source from an external device," while case (2) covers cases of the form "generate the program source programmatically." You could of course consider a hybrid approach like "read the first half of the program from a file and then generate the second half programmatically," but this doesn't seem any different from what you described above.