Hi Scorpion,
PS: Sorry for the late reply, I was kinnda busy, so dint check out CE....... sorry dude!!
The basic idea is this:
It is impossible (in most programming languages) for a program to manipulate itself (i.e. its textual representation — or a representation from which its textual representation can be easily derived) directly.
So to make this possible anyway, we write the build the program from two parts, one which call the code and one which we call the data. The data represents (the textual form of) the code, and it is derived in an algorithmic way from it. The code uses the data to print the code (which is easy because the data represents the code); then it uses the data to print the data (which is possible because the data is obtained by an algorithmic transformation from the code).
I choose C because it is widely known, and also because the printf() function has features which will make writing a quine considerably easier (this is a mixed blessing: it is a gain because it makes the quine smaller, but it also makes it sensibly more obscure and “hackish”).
We will want the quine to be correct C code, so it will probably have to begin something like this:
#include <stdio.h>
int
main (void)
{
The first thing I want to do is print all what precedes. Naively, we could write:
printf("#include <stdio.h>\n\nint\nmain (void)\n{\n");
Then I need to print this line itself:
printf("printf(\"#include <stdio.h>\\n\\nint\\nmain (void)\\n{\\n\");\n");
And so on. It should be obvious that this is not going to work (except if we intend to produce a quine of infinite length, which we do not).
This is the sort of reasoning which makes some people believe that quines don't exist. The problem is that we need to print something, so we use a character string (say s) to print it, and then we need to print s itself, so we use another character string, and so on…
If we intend to print s, we don't need another string: we can use s itself. So I modified and give it another try:
char *s="#include <stdio.h>\n\nint\nmain (void)\n{\n";
printf(s); printf("char *s=\"%s\";\n",s);
Well, it still doesn't work. But we have introduced one of the central ideas in quine-writing lore: whereas it is probably necessary to use some data to represent the code to be printed, on the other hand it is possible to reuse these data to print the data themselves. Here we're still a bit naive: we're using s “as it stands”, but that won't work because it contains some backslashes; these would need to be further backslashified. So the King's way is to proceed with backslashification, which will work because this is a computable process. However, since we are writing in C, we choose a shortcut which uses the nice properties of the printf function:
char *s1="#include <stdio.h>%c%cint%cmain (void)%c{%c";
char *s2=" char *s1=%c%s%c;%c char *s2=%c%s%c;%c";
char n='\n', q='"';
printf(s1,n,n,n,n,n);
printf(s2,q,s1,q,n,q,s2,q,n);
This is a partial quine: it prints the beginning of its own listing (something in no way remarkable, since any program which doesn't print anything is a “partial quine”). Here we have passed the “catching up point”, by this I mean that the program data printed includes the data representation itself. It is then generally trivial to complete the quine and some of the data are actually hidden in the printf() statements. Nevertheless, it is not very difficult to finish, So finally I ended up like this:
#include <stdio.h>
int
main (void)
{
char *s1="#include <stdio.h>%c%cint%cmain (void)%c{%c";
char *s2=" char *s%c=%c%s%c;%c char *s%c=%c%s%c;%c";
char *s3=" char n='%cn', q='%c', b='%c%c';%c";
char *sp=" printf(";
char *s4="%ss1,n,n,n,n,n);%c";
char *s5="%ss2,'1',q,s1,q,n,'2',q,s2,q,n);%ss2,'3',q,s3,q,n,'p',q,sp,q,n);%c";
char *s6="%ss2,'4',q,s4,q,n,'5',q,s5,q,n);%ss2,'6',q,s6,q,n,'7',q,s7,q,n);%c";
char *s7="%ss2,'8',q,s8,q,n,'9',q,s9,q,n);%ss2,'0',q,s0,q,n,'x',q,sx,q,n);%c";
char *s8="%ss3,b,q,b,b,n);%ss4,sp,n);%ss5,sp,sp,n);%c";
char *s9="%ss6,sp,sp,n);%ss7,sp,sp,n);%ss8,sp,sp,sp,n);%c";
char *s0="%ss9,sp,sp,sp,n);%ss0,sp,sp,n,n,n);%c return 0;%c}%c";
char *sx="--- This is an intron. ---";
char n='\n', q='"', b='\\';
printf(s1,n,n,n,n,n);
printf(s2,'1',q,s1,q,n,'2',q,s2,q,n); printf(s2,'3',q,s3,q,n,'p',q,sp,q,n);
printf(s2,'4',q,s4,q,n,'5',q,s5,q,n); printf(s2,'6',q,s6,q,n,'7',q,s7,q,n);
printf(s2,'8',q,s8,q,n,'9',q,s9,q,n); printf(s2,'0',q,s0,q,n,'x',q,sx,q,n);
printf(s3,b,q,b,b,n); printf(s4,sp,n); printf(s5,sp,sp,n);
printf(s6,sp,sp,n); printf(s7,sp,sp,n); printf(s8,sp,sp,sp,n);
printf(s9,sp,sp,sp,n); printf(s0,sp,sp,n,n,n);
return 0;
}
Here we have a real quine. Note the use of the s2 string to print several lines modeled on the same pattern. Also note how the backslash required no special treatment. And note the sx string which goes to show that the classical belief that everything in a quine must be doubled, is false.
however, to the fact that I should have written “const char *” rather than just “char *”; this is much better than many quines which omit the return 0 at the end or similar things.
Hope you understood buddy!!!😒
PS: Thanks Pradeep!!!!!!😁
-Arvind(slashfear)