Jim Lawless' Blog


A Quine in C

Originally published on: Tue, 28 Apr 2009 00:11:12 +0000

A quine is a program that produces its own source as its output.

I'd never written one before, so on Pi Day of this year ( 3/14 ) I gave it a go. Here's the result of my effort. You'll need to scroll waaaay over to the right ...


#include <stdio.h>
#define E(x) putchar(x)
char *s="#include <stdio.h>@10#define E(x) putchar(x)@10char *s=@34~main() {char *p=s;for(;*s;s++){if(*s==64){E((10*(s[1]-48))+(s[2]-48));s+=2;}else if(*s==126){while(*p){E(*p++);} E(34); E(59);E(10);}else{E(*s);}}}";
main() {char *p=s;for(;*s;s++){if(*s==64){E((10*(s[1]-48))+(s[2]-48));s+=2;}else if(*s==126){while(*p){E(*p++);} E(34); E(59);E(10);}else{E(*s);}}}

The issue that I found myself dealing with was the string metacharacters the slash and the double-quote. If I used any of the metacharacters in the source string, I would need to find a way to regenerate the character in its escaped form as well as the raw form.

My solution was to loop through the string s and use my own metacharacter @ to indicate that the next two digits will represent the ASCII character that is to be written. In checking for the metacharacter, I use the ASCII value of @ (64), so if you're trying this out on a machine that uses EBCDIC or something other than ASCII, the code won't work.

The appearance of @10 in the string will cause the program to output a character with an ASCII value of ten ( a newline. ) The appearance of @34 in the string will yield a double-quotation mark. @59 will yield a semicolon.

The next metacharacter my routine uses is the tilde ~ character to denote the end of the prologue of the output. The quine is really three sections wide.

  1. The prologue code ( #include's and such and the beginning of the source string definition... )
  2. The string containing the source code
  3. The body of the program that produces the output.

I abbreviated putchar() as E() with a macro so that the source wouldn't be any lengthier than it needed to be.

To generate the quine, the code first outputs the prologue, then a double-quote, then the complete source string, then another double-quote, then a semicolon, then a preserved section of the source-string that appears after the tilde ( the main() function. )

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Stacking Images with PerlMagick
Next post:Envy


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

A Simple ROT13 Macro

Speeding up JRuby with NailGun

A TCP Command Line Interface in Rhino JavaScript

Changing the C64 Text Color in C

Charging by the Byte

An Interview with Game Developer James Hague

WSH JavaScript Includes

Twimmando No More

Preserving my Favorite HN Links

Blogoversary


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer