• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write a program which outputs its own source code.

 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone have any idea for this? Someone asked this on another forum, so I was curious what people on here thought.... I don't know why people would want to do that, but okay.... :p.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Bear, I forgot about that rule . And yeah this is completely useless, but I'm curious about it....

The only thing I think of is making the entire thing 1 giant string(useless) or making a print statement after each line(useless).... The dude said the code doesn't have to work either, just code, so I figure the first selection but whyyyyyyyyyyyyyyy!!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a name for programs like these: "quine". See Quine (computing) for example - that page has a Java example. If you search for that term you'll probably find plenty of others.

(You can also write a program that reads its own source code and prints it out, but I believe that's considered cheating ).
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should resist the temptation to follow the link Matthew posted, but instead try to do it yourself. The learning effect of doing it yourself far surpasses the knowledge gained by being told how to do it.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:You should resist the temptation to follow the link Matthew posted, but instead try to do it yourself. The learning effect of doing it yourself far surpasses the knowledge gained by being told how to do it.



this isn't for me, it's for someone else :P. I could care less, I was just stumped as how to do it, or WHY to do it :P.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15445
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Orsaw wrote:I don't know why people would want to do that, but okay.... :p.

Just because it's interesting. People try all kinds of new and strange things just because they are curious and they like solving puzzles. Why do people make art?
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a kind of measurement of the power of a programming language if such a quine can be written in it.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To take the challenge one step further (and make the resulting source code even more obscure), try to write the shortest source code that prints itself. I think I have seen a Java solution that does this in less than 200 characters.
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> whyyyyyyyyyyyyyyy!

George Mallory

George Herbert Leigh Mallory (18 June 1886 – 8 or 9 June 1924) was an English mountaineer who took part in the first three British expeditions to Mount Everest in the early 1920s.

Mallory is famously quoted as having replied to the question "Why do you want to climb Mount Everest?" with the retort "Because it's there", which has been called "the most famous three words in mountaineering".
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:To take the challenge one step further (and make the resulting source code even more obscure), try to write the shortest source code that prints itself. I think I have seen a Java solution that does this in less than 200 characters.

Just came across a new language that lets you do it in seven:

chicken

See here
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaja Shinde

Your code only works if:

a) you have the source file
b) the source file is in a VERY specific place.

and in any case, as Matthew wrote back in November, a file that reads its own source code is considered cheating for this challenge.
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger

You are right Sir.
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is source code of the C Program to display its own source code as its output.The C program is successfully compiled and run on a Linux system. The program output is also shown below.

It outputs its own source code

 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjwal soni wrote:Here is source code of the C Program to display its own source code as its output.

Are you saying you could send me your compiled program, I could drop it anywhere on my box, run it, and it would work?

Would your program work on YOUR box if you moved it to a new directory?
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it will work
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjwal soni wrote:Yes, it will work


I think you are missing the point. Will your program work, if you only have the binary? You need to compile your program, and then run it without access to the source code. The binary needs to be able to regenerate it's own source code.

If the OP was looking for your answer, it wouldn't be such a difficult problem to solve, would it?

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your program opens the .c file, reads it line by line, and prints each line. If the .c file isn't there, it won't work. If you give me your binary (and ONLY your binary) and I run it, it won't work.

If you were to edit your .c file, you would print out the new contents of the file, not the code that actually created the binary.

In fact, the 4th post in this thread even states:
(You can also write a program that reads its own source code and prints it out, but I believe that's considered cheating ).
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting....
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well...there would be three ways to do it. First, read the source file and print it. Second, read the source code stored as data in the executable and print it. Third, decompile or disassemble the executable and create source code and print it.

If the first way is cheating, the second is in principle the same thing.

Decompiling would give you something that would "print itself" but it would likely not be the actual source code that was entered. You could run it once, and then compile that output and see if the two matched the second time. I think they probably would.

Disassembling - this would work and would be easy. If you start with assembly language you can know where the opcodes begin and end, and every opcode translates directly to a statement in assembly language. So just read the executable and convert the opcodes back to mnemonics.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic