Program that prints out its own source code?

hamburglar

Platinum Member
Feb 28, 2002
2,431
0
0
Hi, Just out of curiosity, is it possible to write a program that prints out its own source code when executed? Language doesn't matter.

Thanks!
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0
Not sure how you mean.

A program could just print out a text file of its original source code, sure.

It maybe could use a decompiler to decompile itself when ran...
 

hamburglar

Platinum Member
Feb 28, 2002
2,431
0
0
Originally posted by: ThisIsMatt
Not sure how you mean. A program could just print out a text file of its original source code, sure. It maybe could use a decompiler to decompile itself when ran...

That is what I mean. Is it possible in some programming language?
 

ThisIsMatt

Banned
Aug 4, 2000
11,820
1
0
Originally posted by: hamburglar
Originally posted by: ThisIsMatt
Not sure how you mean. <STRONG>A program could just print out a text file of its original source cod</STRONG>e, sure. It maybe could use a decompiler to decompile itself when ran...

That is what I mean. Is it possible in some programming language?
Should be possible in any higher level language. That's not very difficult at all.
 

lukatmyshu

Senior member
Aug 22, 2001
483
1
0
Dude ... don't ask homework questions to CS 70 on ATOT. In any case it can't print out a text file 'cuz the problem statement specifically states that you can't use any file-systems. I'll give you a hint though ... use a #define statement in C.
 

arcas

Platinum Member
Apr 10, 2001
2,155
2
0
I remember this problem from a programming contest back in the early '90s. :D

 

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0
It would be easy just change an Interpperted language like scheme and add in a new special method that will print out the source code. Then call that method from within your program and your all set.
 

hamburglar

Platinum Member
Feb 28, 2002
2,431
0
0
Originally posted by: lukatmyshu
Dude ... don't ask homework questions to CS 70 on ATOT. In any case it can't print out a text file 'cuz the problem statement specifically states that you can't use any file-systems. I'll give you a hint though ... use a #define statement in C.

Your in Cs 70 also? It says we can use the internet...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
This will do it in perl:

#!/usr/bin/perl -s
$;=$/;seek+DATA,!++$/,!$s;$_=<DATA>;$s&&print||$g&&do{$y=($x||=20)*($y||8);sub
i{sleep&f}sub'p{print$;x$=,join$;,$b=~/.{$x}/g}$j=$j;sub'f{pop}sub
n{substr($b,&f%$y,3)=~tr,O,O,}sub'g{$f=&f-1;($w,$w,substr($b,&f,1),O)[n($f-$x)+
n($x+$f)-(substr($b,&f,1)eq+O)+n$f]||$w}$w="\40";$b=join'',@ARGV?<>:$_,$w
x$y;$b=~s).)$&=~/\w/?O:$w)ge;substr($b,$y)=q++;$g='$i=0;$i?$b:$c=$b;
substr+$c,$i,1,g$i;$g=~s?\d+?($&+1)%$y?e;$i-$y+1?eval$g: Do{$i=-1;$b=$c;p;i
1}';sub'e{eval$g;&e}e}||eval||die+No.$;
__DATA__
if($j){{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}}
@s=(q[$_=sprintf+pop@s,@s],q[
if($j){{$^W=$|;*_=sub{$=+s=#([A-z])(.*)#=#$+$1#=g}} #_The_Perl_Journal_#
@s=(q[%s],q[%s])x2;%s;print"\n"x&_,$_;i$j;eval}
])x2;$_=sprintf+pop@s,@s;print"\n"x&_,$_;i$j;eval}$/=$y;$"=",";print
q<#!/usr/local/bin/perl -sw
if(!$s){>.($_=<> ).q<}else{@s=(q[printf+pop@s,@s],q[#!/usr/local/bin/perl -sw
if(!$s){>.(s$%$%%$g,tr=[=[===tr=]=]=||&d,$_).q<}else{@s=(q[%s],q[%s])x2;%s}
])x2;printf+pop@s,@s}
>


Go do your own homework.