My Tech Blog
My Tech Blog :) :)

Quines

A program that generates a copy of its own source text as its complete output.

Self reproducting codes are nice and strange :)

I came across these while killing my time in the internet.

I had no clue how these work at that time. But now think i can explain things .

A C program that prints itself.

1.
Author: Unknown (from The Jargon File)
Notes: The first several examples are variations of the standard one-liner C quines.

main(){char*p=”main(){char*p=%c%s%c;(void)printf(p,34,p,34,10);}%c”;(void)printf(p,34,p,34,10);}

The code is bit of obfuscated. So let us first indent it properly, so that it is alteast readable.

main () {
char *p = “main() { char*p =%c%s%c;(void)printf(p,34,p,34,10);}%c”;
(void)printf(p,34,p,34,10);
}

One thing which you might notice here is that 34 and 10 which happens to be ASCII values ” and new line feed respectively.

Sp now p has “main() { char*p =%c%s%c;(void)printf(p,34,p,34,10);}%c” after the execution of the first line . Now the second line has 4 arguments for the print .

Here the first p which occurs in the printf is the format specifier and others are just the arguments . That is , the %c and %s gets expanded in the first p and not in the others

So the output will be generated like this

printf(p, 34, p, 34, 10 ) where p is “main() { char*p =%c%s%c;(void)printf(p,34,p,34,10);}%c”

So here the %c is replaced by in betweeen is replaced by ” while the final %c is replaced by line feed

So it is now like

main() { char*p =”%s”;(void)printf(p,34,p,34,10);}

When the %s is replaced again by p the final statement becomes
main() { char*p =”main() { char*p =%c%s%c;(void)printf(p,34,p,34,10);}%c”;(void)printf(p,34,p,34,10);}

So that gives the final output .

No Responses Yet to “Quines”

Leave a Reply