Re: A program that reproduces itself

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/16/04


Date: Sat, 16 Oct 2004 20:10:30 GMT


"Robert Rotstein" <rrotstein@verizon.net> wrote in message
news:YUecd.1533$B34.281@trndny02...
> Following is a C program, taken from
> http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C,
> which has the curious property that, when executed, it produces its own
> source code as output.
>
> #include <stdio.h>
> char x[]="#include <stdio.h>%cchar x[]=%c%s%c;%cint main()
> {printf(x,10,34,x,34,10,10);return 0;}%c";
> int main() {printf(x,10,34,x,34,10,10);return 0;}
>
> But I can't figure it out! Specifically, I don't know the significance of
> the hard-coded integers,

They are character encodings (which assume the ASCII character set,
which makes the program nonportable). 10 is the newline character,
34 is the double-quote character(") (for ASCII).

>nor how the % formatting characters work in this
> instance.

They work in the 'normal' way defined by the specification
of the 'printf()' function. %c formats a character output,
%s formats a string (char*) output.

> Can someone explain just how this works?

The array 'x' is the first ('format') argument to 'printf()'
and 10, 34, x, 34, 10, 10 are the subsequent 'printf()' arguments
whose output formatting is specified by the % specifiers in 'x'.

It might help you see what's happening if you change the
'printf()' to 'sprintf()' (whose first argument is a pointer
to a string where the output will be written (instead of to
'stdout' All the other parameters are the same). Follow that
with a 'puts()' of that string. Note that you'll need to
provide the array for 'sprintf()' to write to.

HTH,
-Mike

-Mike



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)