How to write a program which prints its own source code as output?
A Program which reproduces its own source code is called as a
quine.
There are lots of ways to do this, here are some of them:
-
main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}
-
main(){char*a="main(){char*a=%c%s%c;int b='%c';printf(a,b,a,b,b);}";int b='"';printf(a,b,a,b,b);}
You can find more such codes on the web if you search for
quine,
Here is an article written by Gary P. Thompson II∞ which explains how we can do this in many different programming languages.
More such self replicating C language codes can be found at:
http://www.nyx.net/~gthompso/self_c.txt∞
And C++ codes are available at:
http://www.nyx.net/~gthompso/self_c++.txt∞
Visit:
http://www.nyx.net/~gthompso/quine.htm∞ for any other language codes.
CategoryPuzzles