Article View: comp.lang.misc
Article #1590Re: Self-printing C program
From: Kevin D. Quitt
Date: Mon, 17 Sep 1990 15:58
Date: Mon, 17 Sep 1990 15:58
75 lines
2111 bytes
2111 bytes
In article <38724@unix.cis.pitt.edu> meyer@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@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@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.
Technical Details
Message-ID:
<582@demott.COM>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!tut.cis.ohio-state.edu!ucsd!usc!srhqla!demott!kdqReferences:
<38724@unix.cis.pitt.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!tut.cis.ohio-state.edu!ucsd!usc!srhqla!demott!kdq From: kdq@demott.COM (Kevin D. Quitt) Newsgroups: comp.lang.misc,comp.lang.c Subject: Re: Self-printing C program Message-ID: <582@demott.COM> Date: 17 Sep 90 15:58:53 GMT References: <38724@unix.cis.pitt.edu> Reply-To: kdq@demott.COM (Kevin D. Quitt) Followup-To: comp.lang.misc Organization: DeMott Electronics Co., Van Nuys CA Lines: 75