- #1
daniel_i_l
Gold Member
- 868
- 0
Is it possible to write a program (in any language) so that the output is the code of the program itself?
daniel_i_l said:Is it possible to write a program (in any language) so that the output is the code of the program itself?
int main(int argc, char* argv[])
{
string filename = argv[0];
readFile(filename);
}
Borek said:The idea (although not listed in the OP) is that the program doesn't accept any input nor doesn't read external files. Besides, you are making several faulty assumptions.
It will not work - first of all, executable and the source files are different, second, files don't have to be in the same directory.
jablonsky27 said:it will work if u have only 1 source code file(*.cpp) and keep the exe file in the same directory. of course, u ll have to get the filename correctly.
#include <iostream> // Standard header
int main()
{
std::cout << "Hello, world!\n";
std::cout << "#include <iostream> // Standard header\n";
std::cout << "\n";
std::cout << "int main()\n";
std::cout << "{\n";
std::cout << "\n";
std::cout << "std::cout \<\< \"Hello, world!\\n\";\n";
std::cout << "}\n";
}
That's not a program producing it's source code: that's a program asking an external process for some text, and then displaying it.Anhar Miah said:D'oh I just realized the solution is the method one poster above has done, just read a file and print it to screen, but just make sure that file is code itself!
#!/usr/bin/perl
use strict;
sub quote;
$string = <<"HERE";
#!/usr/bin/perl
use strict;
sub quote;
\$string = <<"HERE";
placeholder
\$quoted = quote(\$string);
\$string ~= s/^placeholder\$/\$quoted/m;
print \$string;
sub quote {
my \$s = shift;
\$s ~= s/\\\$/\\\\\\\$/sg;
\$s ~= s/\\\\/\\\\\\\\/sg;
\$s .= "\\nHERE";
return \$s;
}
HERE
$quoted = quote($string);
$string ~= s/^placeholder$/$quoted/m;
print $string;
sub quote {
my $s = shift;
$s ~= s/\$/\\\$/sg;
$s ~= s/\\/\\\\/sg;
$s .= "\nHERE";
return $s;
}
A program that outputs itself is a computer program that produces its own source code as its output. This means that when the program is executed, it will generate a copy of its own code as its result.
Creating a self-referential program can be seen as a fun and challenging coding exercise. It also demonstrates the power and versatility of programming languages, and can be used as a teaching tool for understanding the fundamentals of programming.
From a practical standpoint, a self-referential program serves no real purpose. It does not perform any useful tasks or solve any problems. However, it can be used as a testing tool for compilers or as a demonstration of self-referential systems.
There are many examples of self-referential programs, such as Quines, Brainfuck, and the GNU C Compiler. These programs vary in complexity, but all share the common feature of producing their own source code as their output.
While it is possible to create a program that outputs itself in most programming languages, there are some limitations. For example, the program may need to be written in a specific way or use certain functions in order for it to be self-referential. Additionally, the output may not be an exact copy of the source code due to formatting or language-specific constraints.