Author |
Topic: Self-printing programs (Read 1738 times) |
|
Garzahd
Junior Member
Gender:
Posts: 130
|
|
Self-printing programs
« on: Nov 14th, 2002, 12:38pm » |
Quote Modify
|
I'm surprised that this hasn't made it to the CS section yet; it's a classic. Write a program that prints itself. There's a large number of examples elsewhere on the web, but this is a very interesting problem if you've never tried it before; I recommend giving it a try. Language is up to you... (though using Basic's list command will get you shot for being lame )
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Self-printing programs
« Reply #1 on: Nov 14th, 2002, 12:48pm » |
Quote Modify
|
It's probably cheating, but you can just read in the source/program-file and print it.. Or print every character from memory that's associated whith the program :p (probably not allowed on most operating systems) MOO can do it in a similar way, though you don't have explicit source-files (it's sort of an in-game programming language) You can simply do : for i in (verb_code(this, verb)) player:tell(i); endfor
|
« Last Edit: Nov 14th, 2002, 12:49pm by towr » |
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
Garzahd
Junior Member
Gender:
Posts: 130
|
|
Re: Self-printing programs
« Reply #2 on: Nov 14th, 2002, 1:38pm » |
Quote Modify
|
Yes, reading in the file from disk/memory is also cheating.
|
|
IP Logged |
|
|
|
Jonathan_the_Red
Junior Member
Gender:
Posts: 102
|
|
Re: Self-printing programs
« Reply #3 on: Nov 15th, 2002, 11:44am » |
Quote Modify
|
I whipped this up really quickly... it's kinda chintzy but it works. (hidden by color) char* szFoo; void main(void) { printf(szFoo); putchar(13); putchar(34); printf(szFoo); putchar(34); putchar(';'); } char* szFoo= "char* szFoo; void main(void) { printf(szFoo); putchar(13); putchar(34); printf(szFoo); putchar(34); putchar(';'); } char* szFoo="; Note that despite vBulletin's wordwrapping, the above program should be on two lines.
|
« Last Edit: Nov 15th, 2002, 11:45am by Jonathan_the_Red » |
IP Logged |
My arcade cabinet
|
|
|
TimMann
Senior Riddler
Gender:
Posts: 330
|
|
Re: Self-printing programs
« Reply #5 on: Nov 16th, 2002, 6:57pm » |
Quote Modify
|
The null string isn't a valid program in many languages. For example, in C: [mann@schlep mann]$ cat /dev/null > null.c [mann@schlep mann]$ cc -o null null.c /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function `_start': /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status [mann@schlep mann]$ ./null bash: ./null: No such file or directory [mann@schlep mann]$ Any C program must contain a function named "main". That's not to say that an empty file is not valid C; it's just not a complete program. We can probably have fun quibbling about whether the transcript I gave about proves my point, but I stick to the claim that if you don't implement main(), you don't have a complete C program. 8)
|
|
IP Logged |
http://tim-mann.org/
|
|
|
James Fingas
Uberpuzzler
Gender:
Posts: 949
|
|
Re: Self-printing programs
« Reply #6 on: Nov 18th, 2002, 9:15am » |
Quote Modify
|
Tim, I was going to argue with you, but I think you're right. Logically, the null program does print nothing, but we are asking for source code, which implies a compiler, which of course forces you to implement main (like any C program should). Of course, C is not the best language in which to solve this problem. A much better language is "Quince", which I just invented. The compiler isn't implemented yet, but it will compile every program by translating it into a bunch of printf statements so that it prints its own source code, and compiling that using a C compiler. Now try to write a program that doesn't print out its own source code!
|
|
IP Logged |
Doc, I'm addicted to advice! What should I do?
|
|
|
TimMann
Senior Riddler
Gender:
Posts: 330
|
|
Re: Self-printing programs
« Reply #8 on: Nov 18th, 2002, 2:02pm » |
Quote Modify
|
I don't think they should have. If you look at the makefile for smr.c, it works by *copying* smr.c to smr and doing chmod +x smr. Um, right. Neither the input nor the output is a C program; it's a shell script.
|
|
IP Logged |
http://tim-mann.org/
|
|
|
rahulc
Newbie
Gender:
Posts: 2
|
|
Re: Self-printing programs
« Reply #9 on: Apr 12th, 2004, 8:17pm » |
Quote Modify
|
This should do it: (all on one line, 76 chars total) char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,3 4);} Any C compiler should be able to compile this without problem. It's pure C. Basic idea is to use printf(s, s), where the same string is used as the format string as well as its argument.
|
|
IP Logged |
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: Self-printing programs
« Reply #10 on: Apr 15th, 2004, 6:12am » |
Quote Modify
|
My solution would be to write a program in machine code that prints the code segment. Assembly will not work because because it has assembler directives and other things that either do not make it into the code or are ambiguous when going in reverse. I am sure it is possible, but machine code would be easier. Yes, I have written programs in machine code before. It is ugly and complicated, but possible.
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
|