A challenge for the Advanced

May 1, 2009 at 5:40pm
This in no way is me asking for help on homework or whatever, just for the record! You don't even have to post your code if you don't want to. It is also no my own challenge, i found it somewhere and thought it sounded interesting.

Write a program that, when run, will print out its source code. This source code, in turn, should compile and print out itself.
May 1, 2009 at 5:44pm
This is a challenge? Just open the .cpp file and print it.
May 1, 2009 at 6:01pm
closed account (S6k9GNh0)
Without the implementation file, that's not possible (to my knowledge). When it's compiled, it gets compiled into ASM and then to Machine code. ASM and C++ are not a 1:1 type conversion so the code will just about never be the same.
May 1, 2009 at 6:02pm
I recall that already being done in the obfuscated C programming contest one year.
May 1, 2009 at 6:08pm
These are called Quine programs. You are not allowed any input, so you cannot simply print out the source file. See wikipedia: http://en.wikipedia.org/wiki/Quine_(computing)
May 1, 2009 at 6:43pm
Ah, yes, Quine. These are quite fun to write. Just one word of advice: Don't make a mistake.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

const char *source="\";\n\nint main(){\n\tstd::cout <<\"#include <iostream>\\n\\nconst char *source=\\\"\";\n\tfor (const char *a=source;*a;a++){\n\t\tswitch (*a){\n\t\t\tcase '\\\\':\n\t\t\t\tstd::cout <<\"\\\\\\\\\";\n\t\t\t\tbreak;\n\t\t\tcase '\"':\n\t\t\t\tstd::cout <<\"\\\\\\\"\";\n\t\t\t\tbreak;\n\t\t\tcase '\\t':\n\t\t\t\tstd::cout <<\"\\\\t\";\n\t\t\t\tbreak;\n\t\t\tcase '\\n':\n\t\t\t\tstd::cout <<\"\\\\n\";\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tstd::cout <<*a;\n\t\t}\n\t}\n\tstd::cout <<source;\n\treturn 0;\n}\n";

int main(){
	std::cout <<"#include <iostream>\n\nconst char *source=\"";
	for (const char *a=source;*a;a++){
		switch (*a){
			case '\\':
				std::cout <<"\\\\";
				break;
			case '"':
				std::cout <<"\\\"";
				break;
			case '\t':
				std::cout <<"\\t";
				break;
			case '\n':
				std::cout <<"\\n";
				break;
			default:
				std::cout <<*a;
		}
	}
	std::cout <<source;
	return 0;
}
May 2, 2009 at 2:24am
closed account (S6k9GNh0)
@ HelioS: How is that a Quine program when you hard-coded the source in a char pointer called source and then printed it. If you take the above program and delete everything except the first two and last two lines of code it will print the exact same thing.

Or am I missing the picture....
Last edited on May 2, 2009 at 2:31am
May 2, 2009 at 2:55am
hard-coded the source
That's what a Quine program is, silly.

delete everything except the first two and last two lines of code it will print the exact same thing.
Take a closer look at the string.
(Spoilers below.)












[spoiler]
The string doesn't actually contain the entire source, because it would then need to contain itself. Also note that the string is actually printed twice. Once by a special interpreter, and again directly by std::cout.
[/spoiler]











Last edited on May 2, 2009 at 2:56am
May 2, 2009 at 3:26am
closed account (S6k9GNh0)
>.> That honestly seems pointless to me. >.>

World's smallest Quine program:






I WIN
Last edited on May 2, 2009 at 3:29am
May 2, 2009 at 3:47am
c:/MinGW/bin/../lib/gcc/mingw32/3.4.2/../../../libmingw32.a(main.o)(.text+0x106)
:main.c: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

Maybe it would have worked in Python.
May 2, 2009 at 10:53am
The whole discussion is very interesting. Being a student i was not knowing all this.


Thanks 2 all,

May 2, 2009 at 12:48pm
The whole discussion is very interesting. Being a student i was not knowing all this.


I feel proud lol :)
Topic archived. No new replies allowed.