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.

EE Topic Advisor
LVL 40
evilrixSenior Software Engineer (Avast)Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;

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.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)
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.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

@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.
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.
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,

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.
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.