Can we create a program that print out its source code what if we (or the program itself) compile than the new program would have the same exact functionality what the original program has? So somehow it would reproduce himself in perfect clones that can reproduce too.
The program shouldn't use any external files othervise it would be realy easy to implement: we just need to create a program that reads a file than writes it out, than copy and paste the source code to the external file.
My first attempt was to write a program that has a print function than copy-paste the source code to this print function, but this program can reproduce himeself for only once, and doesn't have the same functionality because the second program cant print out the source code.
-
4This is known as a Quine– user40980Commented Apr 18, 2013 at 22:33
-
Reasons for downvotes are appreciated.– totymedliCommented Apr 18, 2013 at 22:38
-
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask– gnatCommented Apr 18, 2013 at 22:47
-
@gnat I wrote what I tried in the last paragraph, I told why it isn't worked, also I started the question with my needs and what I am curious about.– totymedliCommented Apr 18, 2013 at 22:51
-
1well, somehow, it doesn't feel like you invested serious effort into research. For example, "quine" (and Wikipedia article on it) was first thing that popped up in my web search for words I took from your question: "program that print out its source code". How come that you were unable to find the term and article on the thing you're asking about?– gnatCommented Apr 18, 2013 at 23:03
2 Answers
Yes, as p.s.w.g said this is called a quine.
A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are self-replicating programs, self-reproducing programs, and self-copying programs.
From: http://en.wikipedia.org/wiki/Quine_%28computing%29
And, that can be achieved in any programming language that is turing-complete
see here for more info: https://stackoverflow.com/questions/2568020/is-it-possible-to-create-a-quine-in-every-turing-complete-language
Also, on a related note, here you can find examples of quines and other programming puzzles: https://codegolf.stackexchange.com/search?q=quine
-
would you mind explaining more on what it does and what it's good for? "Link-only answers" are not quite welcome at Stack Exchange– gnatCommented Apr 18, 2013 at 22:53
-
Ok, I edited my answer, do you mind removing the down vote?– jsedanoCommented Apr 18, 2013 at 23:05