468,512 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 3292
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hung Jung Lu | last post: by
5 posts views Thread by aleksander.helgaker | last post: by
5 posts views Thread by Henry Jordon | last post: by
4 posts views Thread by yinglcs | last post: by
9 posts views Thread by sandy | last post: by
Banfa
2 posts views Thread by Banfa | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.