Self-printing C program
Kevin D. Quitt
kdq at demott.COM
Tue Sep 18 01:58:53 AEST 1990
In article <38724 at unix.cis.pitt.edu> meyer at unix.cis.pitt.edu (Timothy S. Meyer) writes:
>
>Seems to me I saw a self-printing C program a few years back in CACM or
>Computer magazine, but I can't seem to find it. Can anyone give me a
>reference for such a program? (A self-printing program is one that
>outputs a copy of its own text and is more of a recreational interest I
>think.)
>
>Any help will be appreciated.
/*
This solution comes from the Winter 1989 edition EUUG newsletter. It appears
in Puzzle Corner edited by Mick Farmer (mick at cs.bbk.ac.uk). No author is
indicated.
*/
#define QUOTE '"'
#include <stdio.h>
int main()
{
int i,j;
extern char *s[];
s[0][15] = QUOTE;
for (i = 0; *s[i] != '$'; i++)
puts(s[i]);
s[0][15] = '$';
for (j = 0; s[j]; j++) {
putchar(QUOTE);
fputs(s[j], stdout);
putchar(QUOTE);
putchar(',');
puts(s[2]);
}
for (i++; s[i]; i++)
puts(s[i]);
}
char *s[] = {
"#define QUOTE '$'",
"#include <stdio.h>",
"",
"int main()",
"{",
" int i,j;",
" extern char *s[];",
" s[0][15] = QUOTE;",
" for (i = 0; *s[i] != '$'; i++)",
" puts(s[i]);",
" s[0][15] = '$';",
" for (j = 0; s[j]; j++) {",
" putchar(QUOTE);",
" fputs(s[j], stdout);",
" putchar(QUOTE);",
" putchar(',');",
" puts(s[2]);",
" }",
" for (i++; s[i]; i++)",
" puts(s[i]);",
"}",
"",
"char *s[] = {",
"$",
"NULL };",
NULL };
Have fun!
--
_
Kevin D. Quitt demott!kdq kdq at demott.com
DeMott Electronics Co. 14707 Keswick St. Van Nuys, CA 91405-1266
VOICE (818) 988-4975 FAX (818) 997-1190 MODEM (818) 997-4496 PEP last
96.37% of all statistics are made up.
More information about the Comp.lang.c
mailing list