Write a program that prints out its own source code



Frequently a question of the programs printing out their own output appears in code interviews. The question does not really test a skill of a programmer, as much as general awareness of tricks to do that. The quine page by Gary Thompson provides many examples of such applications in various languages. In case you were wondering, a program whose output is its own source code is called a quine, something that might come of use over the next interview. The aforementioned Quine Page lists the following example for C:
char*f=”char*f=%c%s%c;main()
{printf(f,34,f,34,10);}%c”;
main(){printf(f,34,f,34,10);}
One language that’s missing, however, is PHP. In PHP a helpful function and __FILE__ constant allow for this quick hack:
echo file_get_contents(__FILE__);



Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: