Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
4  
This is known as a Quine –  MichaelT Apr 18 '13 at 22:33
    
Reasons for downvotes are appreciated. –  totymedli Apr 18 '13 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 –  gnat Apr 18 '13 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. –  totymedli Apr 18 '13 at 22:51
1  
well, 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? –  gnat Apr 18 '13 at 23:03
show 1 more comment

2 Answers

up vote 8 down vote accepted

That's called a quine. It's a frequent subject of programming challenges.

This page lists a large number of quines written in various languages. You can look at how it's implemented in those examples to get the general idea of how to write your own.

share|improve this answer
add comment

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: http://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: http://codegolf.stackexchange.com/search?q=quine

share|improve this answer
    
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 –  gnat Apr 18 '13 at 22:53
    
Ok, I edited my answer, do you mind removing the down vote? –  jsedano Apr 18 '13 at 23:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.