This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programming Diversions and the fly likes Write a program which outputs its own source code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Write a program which outputs its own source code. " Watch "Write a program which outputs its own source code. " New topic
Author

Write a program which outputs its own source code.

Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

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

Joined: Jun 14, 2011
Posts: 356

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

Joined: Apr 06, 2010
Posts: 4425
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
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.


Ping & DNS - my free Android networking tools app
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

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

Joined: Aug 16, 2005
Posts: 14278
    
  21

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
It is a kind of measurement of the power of a programming language if such a quine can be written in it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
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

Joined: May 22, 2012
Posts: 867
    
    5
> 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

Joined: Oct 02, 2003
Posts: 11441
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Pankaja Shinde
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

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

Joined: Sep 15, 2006
Posts: 87
fred rosenberger

You are right Sir.
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
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



Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

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

Joined: Mar 28, 2007
Posts: 403
Yes, it will work
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

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

Joined: Mar 08, 2014
Posts: 291
    
    4

Interesting....
Guillermo Ishi
Ranch Hand

Joined: Jul 28, 2014
Posts: 115
    
    1
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write a program which outputs its own source code.