C / C++ FAQs & Programming Resources - ProkutFAQ : Cquine

HomePage Recent Changes Recently Commented Login/Register
Quick Links
Categories
Links

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
 Comments [Hide comments/form]
how exactly does the quine work???
cant understand the code
-- 209.190.85.92 (2007-01-19 09:27:58)
ok that's a very nice code.......... i think many quine codes are available...... wat's the use of QUINES??
-- 203.153.35.72 (2007-12-22 06:41:41)
Hey all.. Anyone plz tell how this quine works?
-- AishWarya (2007-12-22 09:13:38)
Hi everyone !
If u want to know how this works then u can check out my post in topic "C Program that prints itself !!!" on orkut in the community "It's Hard-n-Soft-Ware World".
community link=http://www.orkut.co.in/Main#Community.aspx?cmm=25703011

Or if you are not an orkut user then i can explain that here!
-- NeelNik (2009-01-10 09:19:29)
Well i think it's better to explain this here....

This type of functions are called Quines which prints their own coding .......

Now I'm gonna define it line by line ...
1st line(except the line "main(){")
char *a = "main(){char *a = %c%s%c; int b = '%c'; printf(a,b,a,b,b);}";

this line tells us that we are trying to allocate a string "main(){char *a = %c%s%c; int b = '%c'; printf(a,b,a,b,b);}" into memory and stores the pointer to its first character to the char *a(character pointer variable a).........

2nd line :
int b=' " ';

this line tells us that we are assigning a character constant " to a integer variable b ...It's right because c supports type mixing of variables and cast that type implicitly.......Now what will be stored in b? B will store a value of 34 which is the ASCII value of " ........

3rd line :
printf(a,b,a,b,b);
Now before going to 3rd line i want to ask a question!!
How printf function works ?? What is it's prototype??

The prototype of printf function as declared in the "stdio.h" header file is much like -
int printf(const char _FAR *__format, ...);

it means that printf function has variable number of arguments as the three dots "..."
in the arguments means that in ellipsis notation

it also states that the first argument is the constant char far pointer (*) of a formatted string .and then the variable number of arguments call(Its for the format specifiers we wrote like %c,%d etc). It also have a return type of integer value. It actually returns the length of string passed as the 1st argument.
Now the third line lets see what it says -
printf(a,b,a,b,b);
it actually passes the char pointer *a as its 1st argument so the printf function is going to print the line "main(){char *a = %c%s%c; int b = '%c'; printf(a,b,a,b,b);}" except the double quotation marks..

then it comes to "format specifiers"(%c%s%c .. %c) it tells that there should be something extra and the latter 4 arguments just tell that what is going to be printed on that position. So the value of b then a(as a string) then b and b is printed on their respective positions.

And the total printing comes out like this 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);}


Last word =
Those who don't know what %c and %s means it's here=
Format Specifier Meaning
%c -------------------------- one character
%s--------------------------- a string
-- NeelNik (2009-01-10 09:23:21)
I hope now you can find out the meaning of the 1st one!
-- NeelNik (2009-01-10 09:29:18)
but the header files not comes as output in this program........
i want they also appear in output...........
-- 117.199.160.248 (2009-10-26 00:40:41)
Page was generated in 0.0388 seconds