Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Heres a good puzzle: how do you write a program which produces its own source code as output?

0
Posted

Heres a good puzzle: how do you write a program which produces its own source code as output?

0

It is actually quite difficult to write a self-reproducing program that is truly portable, due particularly to quoting and character set difficulties. Here is a classic example (which ought to be presented on one line, although it will fix itself the first time it’s run): char*s=”char*s=%c%s%c;main(){printf(s,34,s,34);}”; main(){printf(s,34,s,34);} (This program has a few deficiencies, among other things neglecting to #include , and assuming that the double-quote character ” has the value 34, as it does in ASCII.) Here is an improved version, posted by James Hu: #define q(k)main(){return!puts(#k”\nq(“#k”)”);} q(#define q(k)main(){return!puts(#k”\nq(“#k”)”);}) comp.lang.c FAQ list ยท Question 20.35 Q: What is “Duff’s Device”? A: It’s a devastatingly devious way of unrolling a loop, devised by Tom Duff while he was at Lucasfilm. In its “classic” form, it was used to copy bytes, and looked like this: register n = (count + 7) / 8; /* count > 0 assumed */ switch (count % 8) { ca

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123