I think this might be a classic question but I am not aware of an answer. Can a program output a copy of itself, and, if so, is there a short program that does this?
I do not accept the "empty program" as an answer, and I do not accept programs that have access to there own source code. Rather, I am thinking something like this:
int main(int argc, char** argv){ printf("int main(argc, char** argv){ printf...
but I do not know how to continue...
If you write a quine, be careful that the copies don't also write copies of themselves ad infinitum and end up taking over the world.
It's called a quine, and there's a site that collects them.
This is indeed a classic question!
Beyond the existence of specific quines, an important result in computability theory is that for any function you might want to compute, there exists a program that "knows its own program text", i.e. that could print itself if desired. This theorem is called Kleene's second recursion theorem.
In the language invented by Jon Skeet the following operator prints "Hello, world!\n".
I can make a modification of this language so that the following program prints "Hello, world!\n":
So that's the program that prints itself.
Oh, you feel something strange about it, while it has a precise and correct mathematical definition? That's your problem. "I won't accept..." ha! Mathematics does accept, and she's the mistress I serve, so I post this answer.
This is called a Quine:
Source: Wikipedia