Thursday, April 03, 2008

"print myself"

Some years ago, a friend told me about a C program he wrote that printed itself. That is, when you run the program, it prints its source. Pretty cool, huh?

For the über-geeks out there, what I mean is this:

$ cc print_me.c -o print_me
$ ./print_me > print_me2.c
$ diff print_me.c print_me2.c
$ echo $?
0
$


I was waiting (for/on?) a train or something a few months ago and decided to try to reconstruct the program. I won't put it here; you can try it for a while and then take a look at print_me.c

Well, that's not the prettiest thing I've ever seen; there is probably a much more elegant solution too, but I don't recall what it was.

No comments: