I found this thing pretty interesting and its worth sharing. Quine, by definition, is a kind of program that produces itself, i.e. if you execute the code, it will print itself.

Some may say that we can open the file where the program is written from within the program itself and thus print it character by character. Yea, it will work, but there are several others methods too.

Here is the code with the idea of opening the file where the code is written:


#include <stdio.h>
main(){
int c;
FILE *f;
f = fopen( __FILE__, "r" );
c = fgetc( f );
while( c != EOF ) {
putchar( c );
c = fgetc( f );
}
fclose( f );
}

Now, surely, you won’t like this kind of technique. So, here goes another implementation:


p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}

This one is pretty interesting and it works fine with GNU C Compiler.

Now here is one more interesting code I found on Internet that prints the code itself, but this time, in reverse order.


main(){char*a="main(){char*a=c%s%c%;printf(a+42,34,a,34);};)43,a,43,24+a(ftnirp;%c%s%c=a*rahc{)(niam";printf(a+42,34,a,34);}

AND here goes the ultimate one:

This program prints itself and the most noticeable part of the code is that it’s a palindrome!!!! Dan Hoey made this possible..


/**/main(){char*a=/*/};)q,q,a,q,q,811+a(ftnirp;'"'=q,/**/"/**/main(){char*a=/*/};)q,q,a,q,q,811+a(ftnirp;'c%'=q,/**/c%s%c%**/,q='c%';printf(a+118,q,q,a,q,q);}/*/=a*rahc{)(niam/**/main(){char*a=/*/};)q,q,a,q,q,811+a(ftnirp;'%c'=q,/**/%c%s%c/**/,q='%c';printf(a+118,q,q,a,q,q);}/*/=a*rahc{)(niam/**/"/**/,q='"';printf(a+118,q,q,a,q,q);}/*/=a*rahc{)(niam/**/

Now, I would ask for some help. Can you help me writing a shellscript which will be a quine!