wu :: forums
« wu :: forums - Self-printing programs »

Welcome, Guest. Please Login or Register.
May 19th, 2024, 8:54am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: towr, Icarus, ThudnBlunder, Grimbal, Eigenray, SMQ, william wu)
   Self-printing programs
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Self-printing programs  (Read 1738 times)
Garzahd
Junior Member
**





    mlahut


Gender: male
Posts: 130
Self-printing programs  
« on: Nov 14th, 2002, 12:38pm »
Quote Quote Modify 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 Smiley )
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Self-printing programs  
« Reply #1 on: Nov 14th, 2002, 12:48pm »
Quote Quote Modify 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
**





    mlahut


Gender: male
Posts: 130
Re: Self-printing programs  
« Reply #2 on: Nov 14th, 2002, 1:38pm »
Quote Quote Modify Modify

Yes, reading in the file from disk/memory is also cheating.
IP Logged
Jonathan_the_Red
Junior Member
**





   
Email

Gender: male
Posts: 102
Re: Self-printing programs  
« Reply #3 on: Nov 15th, 2002, 11:44am »
Quote Quote Modify 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
Jeremiah Smith
Full Member
***



Beep!

   


Posts: 172
Re: Self-printing programs  
« Reply #4 on: Nov 16th, 2002, 3:23pm »
Quote Quote Modify Modify

Self-printing programs are also called "quines":
 
http://www.tuxedo.org/jargon/html/entry/quine.html
 
Incidentally, the shortest quine is a null string, it prints nothing, but since it IS nothing, it prints itself Smiley
IP Logged
TimMann
Senior Riddler
****






   
WWW

Gender: male
Posts: 330
Re: Self-printing programs  
« Reply #5 on: Nov 16th, 2002, 6:57pm »
Quote Quote Modify 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
*****





   
Email

Gender: male
Posts: 949
Re: Self-printing programs  
« Reply #6 on: Nov 18th, 2002, 9:15am »
Quote Quote Modify 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?
Jeremiah Smith
Full Member
***



Beep!

   


Posts: 172
Re: Self-printing programs  
« Reply #7 on: Nov 18th, 2002, 11:40am »
Quote Quote Modify Modify

Darn... well, the International Obfuscated C Code Contest judges seemed willing enough to accept it Cheesy
 
http://www0.us.ioccc.org/years.html#1994_smr
IP Logged
TimMann
Senior Riddler
****






   
WWW

Gender: male
Posts: 330
Re: Self-printing programs  
« Reply #8 on: Nov 18th, 2002, 2:02pm »
Quote Quote Modify 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
*





   
WWW

Gender: male
Posts: 2
Re: Self-printing programs  
« Reply #9 on: Apr 12th, 2004, 8:17pm »
Quote Quote Modify 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!

5187759 5187759   john23874   SnowmanJTG
WWW Email

Gender: male
Posts: 767
Re: Self-printing programs  
« Reply #10 on: Apr 15th, 2004, 6:12am »
Quote Quote Modify 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
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board