Self printing code  

 

Introduction

Have you ever tough about a program that prints its own code? If not you should try before getting the solution below. It is not as easy as you can imagine.

Rules : You may not read any file. For example if your source file is printMY.java, you cannot load the file and print it. You may not make a pointer that goes from beginning to end of your program in RAM and print it. In other words, your program may only read variables that it created.

 

Why is it not trivial?

Let's suppose you have a language that implements the function "print" which would work so if you would like to print "Mr Yellow" :

print("Mr Yellow");

Now make a program that prints nothing :

print("");

As we don't want to print nothing but our own code, we have to put our code inside the quotes :

print("print("");");

But as we modified the source code adding code inside quotes, we have to correct the code to print :

print("print("print("");");");

And so on...

 

There is a solution

Click here to get the solution.

 

 
 
 

Copyright by Michel Yerly