Re: A program that reproduces itself
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/16/04
- Next message: Arthur J. O'Dwyer: "Re: A program that reproduces itself"
- Previous message: Robert Rotstein: "A program that reproduces itself"
- In reply to: Robert Rotstein: "A program that reproduces itself"
- Next in thread: Mike Wahler: "Re: A program that reproduces itself"
- Reply: Mike Wahler: "Re: A program that reproduces itself"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Arthur J. O'Dwyer: "Re: A program that reproduces itself"
- Previous message: Robert Rotstein: "A program that reproduces itself"
- In reply to: Robert Rotstein: "A program that reproduces itself"
- Next in thread: Mike Wahler: "Re: A program that reproduces itself"
- Reply: Mike Wahler: "Re: A program that reproduces itself"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|