473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

A program which would print out it's own source code - possible ornot?


Is it possible to write a program which would print out it's own
source code, using C++?
Jan 23 '08 #1
21 3539
mi******@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
Yes.

- Jensen
Jan 23 '08 #2
On 23 Jan, 11:15, mikha...@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
Google "quine".
Jan 23 '08 #3
mi******@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
yes, here is one example for free:
Jan 23 '08 #4
On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid:
mi******@gmail.com wrote:
>Is it possible to write a program which would print out it's own
source code, using C++?

yes, here is one example for free:
deep, philosophically... lol.

--

-kira

Jan 23 '08 #5
Kira Yamato wrote:
On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid:
>mi******@gmail.com wrote:
>>Is it possible to write a program which would print out it's own
source code, using C++?

yes, here is one example for free:

deep, philosophically... lol.
It probably is. But my dumb compiler complains that there is no
'main' in my program...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 23 '08 #6
mi******@gmail.com writes:
Is it possible to write a program which would print out it's own
source code, using C++?
Yes, that's trivial.

--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
Jan 23 '08 #7
"Victor Bazarov" <v.********@comAcast.netwrites:
Kira Yamato wrote:
>On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid:
>>mi******@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?

yes, here is one example for free:

deep, philosophically... lol.

It probably is. But my dumb compiler complains that there is no
'main' in my program...
Ok, so you need at least a main.

int main(){
// add something here...
return 0;
}
--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
Jan 23 '08 #8
Pascal Bourguignon wrote:
"Victor Bazarov" <v.********@comAcast.netwrites:
>Kira Yamato wrote:
>>On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid:

mi******@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?

yes, here is one example for free:

deep, philosophically... lol.

It probably is. But my dumb compiler complains that there is no
'main' in my program...

Ok, so you need at least a main.

int main(){
// add something here...
return 0;
}
Weird. This one doesn't print its own source code. Why?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 23 '08 #9
Victor Bazarov wrote:
Pascal Bourguignon wrote:
>"Victor Bazarov" <v.********@comAcast.netwrites:
>>Kira Yamato wrote:
On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid:

mi******@gmail.com wrote:
>Is it possible to write a program which would print out it's own
>source code, using C++?
yes, here is one example for free:
deep, philosophically... lol.
It probably is. But my dumb compiler complains that there is no
'main' in my program...
Ok, so you need at least a main.

int main(){
// add something here...
return 0;
}

Weird. This one doesn't print its own source code. Why?

V
Allow me to correct Pascal's code:

int main()
{
// add something here to print its own source.
// this is left as an exercise to the reader
return 0;
}

Jan 23 '08 #10
Victor Bazarov wrote:
Kira Yamato wrote:
>On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid:
>>mi******@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
yes, here is one example for free:
deep, philosophically... lol.

It probably is. But my dumb compiler complains that there is no
'main' in my program...
You need to tweak it a bit (different compiler options). My linker
complains:
(.text+0x18): undefined reference to `main'
:(
Jan 24 '08 #11
On Jan 23, 10:41 pm, "Bo Persson" <b...@gmb.dkwrote:
Victor Bazarov wrote:
Kira Yamato wrote:
On 2008-01-23 10:24:30 -0500, anon <a...@no.nosaid:
>mikha...@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
>yes, here is one example for free:
deep, philosophically... lol.
It probably is. But my dumb compiler complains that there is no
'main' in my program...
But other compilers actually once compiled this, ran it, and produced
an identical empty output.
For what system? In a hosted environment, a C++ program must
contain a function main somewhere.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 24 '08 #12
kwikius <an**@servocomm.freeserve.co.ukwrites:
>When I tried it, it worked in the directory where I compiled it,
but when I copied it to ~/bin, and invoked it from there, it
output an earlier suggestion---the one without a main.

Yes! It was hard solving that one as well but I figured it eventually.
Isnt that even more useful than the original application?... its
multipurpose :-)
But it's not what's specified.

So how could you write a program that writes a copy of its source,
without relying on run-time external files other than the executable,
since they may not be available?

--
__Pascal Bourguignon__
Jan 24 '08 #13
On Jan 23, 6:15*am, mikha...@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
That's old. The trick is to get the compiler
to spit out the entire source code from
messages produced during compilation.
Socks
Jan 24 '08 #14
red floyd wrote:
>
Allow me to correct Pascal's code:

int main()
{
// add something here to print its own source.
// this is left as an exercise to the reader
return 0;
}
Non-portably and as a first thought I can think of this for my system:
#include <cstdlib>

int main()
{
std::system("cat main.cc");
}
It outputs:

[john@localhost src]$ ./foobar-cpp
#include <cstdlib>

int main()
{
std::system("cat main.cc");
}

[john@localhost src]$
Jan 24 '08 #15
Ioannis Vranos wrote:
>
Non-portably and as a first thought I can think of this for my system:
#include <cstdlib>

int main()
{
std::system("cat main.cc");
}
It outputs:

[john@localhost src]$ ./foobar-cpp
#include <cstdlib>

int main()
{
std::system("cat main.cc");
}

[john@localhost src]$

This can be improved my specifying int main(int argc, char **argv),
taking as an argument the source file. The program requirements doesn't
specify that arguments are prohibited.

But regarding portability, that isn't portable too.
Jan 24 '08 #16
mi******@gmail.com a écrit :
Is it possible to write a program which would print out it's own
source code, using C++?
I am not very confident with this but I think there are debugging
options or formats that embedded the source code in the executable... If
a programm is able to display these informations it surely can be
executed againsts itself.
Jan 24 '08 #17
On Jan 24, 2:03*pm, Pascal Bourguignon <p...@informatimago.comwrote:
Puppet_Sock <puppet_s...@hotmail.comwrites:
On Jan 23, 6:15*am, mikha...@gmail.com wrote:
Is it possible to write a program which would print out it's own
source code, using C++?
That's old. The trick is to get the compiler
to spit out the entire source code from
messages produced during compilation.

Then it wouldn't be the program that would print out its own source
code, it would be the compiler.
You know, I never get the memos. When did the memo
go around that we were all supposed to be idiots?

Me: The trick is to get the compiler to do it.
You: Then it would be the compiler doing it.
Me: (Does my Benny Hill imitation, slapping you
repeatedly on the back of the head like
B.H. did that little bald guy.)
Socks
Jan 24 '08 #18
I cannot see why it would be impossible to write a decompiler that was
carefully coded to decompile itself to its own source.
Is there some computation theory that I am missing?
Is it possible to write a program which would print out it's own
source code, using C++?

Jan 24 '08 #19
On Jan 24, 4:16*pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
kwikius <a...@servocomm.freeserve.co.ukwrites:
When I tried it, it worked in the directory where I compiled it,
but when I copied it to ~/bin, and invoked it from there, it
output an earlier suggestion---the one without a main.
Yes! It was hard solving that one as well but I figured it eventually.
Isnt that even more useful than the original application?... its
multipurpose :-)

But it's not what's specified.
From the O.P....

"Is it possible to write a program which would print out it's own
source code, using C++? "
As explained above I've even thrown in some very useful extra
functionality!! :-)

regards
Andy Little
Jan 25 '08 #20
On Thu, 24 Jan 2008 20:03:20 +0100, Pascal Bourguignon wrote:
Puppet_Sock <pu*********@hotmail.comwrites:
>On Jan 23, 6:15Â*am, mikha...@gmail.com wrote:
>>Is it possible to write a program which would print out it's own
source code, using C++?

That's old. The trick is to get the compiler to spit out the entire
source code from messages produced during compilation.

Then it wouldn't be the program that would print out its own source
code, it would be the compiler.
Right. So if you ran a compiled executable of a program that "prints out
its own source code" then I guess it wouldn't be the program that prints
out its own source code, it would be the run-time system.

--
Lionel B
Jan 25 '08 #21
Matt wrote:
>
I looked at some of those examples of a quine the other day. It looked like
there was a constraint where you could not have any input files. So, a
decompliler that took itself as input would not be legal.
The other thing I noticed is they seem to cheat by using printf. Why is it
legal to use external sources in libraries and not also print the source for
those libraries?

Interesting question. However any standard library headers #included, in
reality they can be like a switch, with no actual header file existing.
Jan 25 '08 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
1
by: Hung Jung Lu | last post by:
Hi, I have been looking into AOP (Aspect-Oriented Programming) for sometime, now. I frankly don't like the syntax of any of the approaches I have seen so far. I am kind playing around with some...
5
by: aleksander.helgaker | last post by:
I've completely rewritten a calculator I wrote to help me learn Python. After someone told me about the def command I reliesed that I could make the program much better, but there is a very anoying...
5
by: Henry Jordon | last post by:
hello I was wondering if someone could help me get a main going on this project I've completed the header file that the professor started us on but not really sure how to get the main going. If...
4
by: yinglcs | last post by:
I am trying to debug a C++ program in GDB on linux. I want to dump out the content of the "this" object, Here is what I get: (gdb) print *this $2 = {path = <incomplete type>} My question is...
9
by: sandy | last post by:
Hey guys, I know this is silly, but I just wanted to take some break from work. I was just wodering if its possible to write a c++ program that can print "iloveyou" in shape of 'heart' when...
12
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.