Tuesday 18 March 2014

Tagged Under:

Quine - Print its own Source Code as Output

Share
How Many Of You Know That You Can Also Print Your Source Code As An Output Using Quine.

What Is A Quine ??


A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".

Quine – A self-reproducing program


A quine is a program which prints a copy of its own as the only output. A quine takes no input. Quines are named after the American mathematician and logician Willard Van Orman Quine (1908–2000). The interesting thing is you are not allowed to use open and then print file of the program.

An example of a quine in C language is given below which produces the source code as an output.

To the best of our knowledge, below is the shortest quine in C.

Source Code
main()
{
    char *s="main() { char *s=%c%s%c; printf(s,34,s,34); }";
    printf(s,34,s,34);
}



Output


This program uses the printf function without including its corresponding header (#include ), which can result in undefined behavior. Also, the return type declaration for main has been left off to reduce the length of the program. Two 34s are used to print double quotes around the string s.

NOTE: It can be written in any other language like java, python, etc.

And in Python:


s = 's = %r\nprint(s%%s)'
print(s%s)


And in Ruby:

eval s="print 'eval s=';p s"

If you find a shorter C quine or you want to share quine in other programming languages, then please do write in the comment section.

0 comments:

Post a Comment

Contact Us

Want to ask anything? Be our guest, give us a message.

Name Email * Message *