Write a program to output it's own source code using ANSI C/C++ (puzzle for experts)

This isn't really a question; rather, it is a puzzle!!!

Please note, this is NOT an assignment of any kind (check my profile if you are unsure as to my tenure on the site). The idea is to spark some activity in the C/C++ topic areas and to have a little fun. Points will be awarded to the expert(s) who provide the most interesting solution. The idea is to have a little fun, promote discussion and have something interesting to think/talk about.

If this goes well, I plan on doing one of these types of puzzle questions on a regular(ish) basis. I would like to see expert colaboration, rather than experts just going gun-ho to get points. I will consider colaboration between experts when I finally award points. I will close this question once I believe it has reached a natural conclusion (ie. no futher productive activity).

So, let's start with the first puzzle. It's one that has been set many times before and I'd be interested in seeing different approaches to solving it. Please don't just search Google and copy/paste. I promise I will check and any plaigirised answers will be ignored (and probably deleted).

Let's go...

Your job is to write a program that, when run, outputs it's own source code (including comments) such that if the output was fed back into a compiler, built and run it would do exactly the same thing as the original program. The output must be word for word identical to the original source code, such that performing a diff between the original source code and the output would show zero differences.

You may use any feature of the latest C/C++ standards, but you must NOT use any 3rd party libraries or Operating System specific code. Basically, your code needs to be 100% ANSI standard and should build, run and behave correctly using any standards complient compiler (gcc, clang, Visual Studio [*cough*]). You must NOT use embedded assembly nor may you use any compiler tricks that the ANSI standards state would be undefined or unspecified behaviour.

Ideally, I would like to see the full source code posted in the comment along with a link to your working solution using the https://ideone.com/ online compiler. You may use either C or C++ as long as your code is standards complient, with the very latest standards for C/C++.

I look forward to seeing your submissions.

evilrix
EE Topic Advisor
LVL 40
evilrixSenior Software Engineer (Avast)Asked:
Who is Participating?
 
Paul KentHigh School StudentCommented:
Well, somebody's got to be the one to post the obvious answer:
#include <iostream>
#include <fstream>

using namespace std;
int main(int argc, char *argv[]) {
	string line;
	ifstream myFile("CognitoErgoSum.cpp");
	while(getline(myFile,line)) {
		cout << line << endl;
	}
	myFile.close();
}

Open in new window

Open the file (whatever it's called) and read out its contents. (Given that this is filename-sensitive, though, this code doesn't work on ideone.com.)
0
 
evilrixSenior Software Engineer (Avast)Author Commented:
Paul, what if you don't have the original source code? I probably should have mentioned that in the original question :)
0
 
Dave BaldwinFixer of ProblemsCommented:
You can't do it without the original source code.  The optimizations that any decent C/C++ compiler makes it very difficult if not impossible to recreate the C/C++ source.  You disassemble it into the ASM code but that won't look like or show the original C code.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
peprCommented:
@Dave Baldwin: It is possible to write such programs (in many programming languages).

I am not going to participate. Anyway, it is easy to google the solutions. If you want to have fun, do not cheat and do not look for "quine". ;)

On the other hand, it is fun even to have a look at the solutions.
0
 
evilrixSenior Software Engineer (Avast)Author Commented:
@Dave, pepr is right - it is possible and this type of program is called a "quine". I left that out of the question to make it more of a challenge. I'm interested to see what type of solutions experts can come up with. Mostly, I just wanted to get some activity going in the C/C++ zones, which seem to be pretty quiet these days.

If anyone has any ideas for future puzzles feel free to message me.
1
 
evilrixSenior Software Engineer (Avast)Author Commented:
It's a shame that no more experts decided to take up this challenge, but I hope the next one will be more interesting. Since two experts provided answers that could be considered solutions I am accepting them both.

Thank you all for participating in this little experiment. If you have any ideas for future puzzles please feel free to message me direct.

All the best,

evilrix
0
 
peprCommented:
The world becomes too fast for "deep thinking". Too much programmers, too much just surface scratching, framework-based certificates. The programming humanity becomes more lazy and dumber. Myself included.
0
 
evilrixSenior Software Engineer (Avast)Author Commented:
pepr, I'd really welcome an idea for another puzzle if you have one... or ask one yourself as I'd love to participate :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.