473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A program that reproduces itself

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, nor how the % formatting characters work in this
instance. Can someone explain just how this works?
Nov 14 '05 #1
10 1862

"Robert Rotstein" <rr*******@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
Nov 14 '05 #2

On Sat, 16 Oct 2004, Robert Rotstein wrote:

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.
Google "C quine" for many, many more.
#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're the ASCII-encoded values of the characters '\n' (10) and '"'
(34). The programmer is relying on the assumption that this program will
only ever be run on implementations using the ASCII character encoding,
which---while generally a reasonable assumption---means that the program
is not a strictly conforming C program.
(The string literal also has an embedded newline, which is definitely
not valid C, but that's just an artifact of your posting technique; the
original program on Wikipedia doesn't have that defect.)
nor how the % formatting characters work in this
instance.
Same way they always do: %s prints a string, %c prints a character.
What don't you understand about them?
Can someone explain just how this works?


Just run through it, with the knowledge that when the programmer writes
"10", he's expecting to see a newline in the output, and when he writes
"34", he's expecting to see a double quote.

-Arthur

Blatant plug: http://www.contrib.andrew.cmu.edu/~a...ftware/quine.c
Nov 14 '05 #3

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:We*****************@newsread3.news.pas.earthl ink.net...
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).


Also note that the code can be easily rendered portable:

Replace those ASCII values with character literals.
( '\n' and '"' )

-Mike
Nov 14 '05 #4
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi**********************************@unix41.a ndrew.cmu.edu...
(The string literal also has an embedded newline, which is definitely
not valid C, but that's just an artifact of your posting technique; the
original program on Wikipedia doesn't have that defect.)


Darn! I missed that. Eagle eyes! :-)

-Mike
Nov 14 '05 #5
In article <YUecd.1533$B34.281@trndny02>,
"Robert Rotstein" <rr*******@verizon.net> wrote:
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, nor how the % formatting characters work in this
instance. Can someone explain just how this works?


The numbers are various ASCII codes: 10 is '\n' (newline), and 34
is '"' (double quote). The "%c" format specifier says to insert the
next argument as a character, and the "%s" says to print the next
argument as a string. Unfortunately, it can't be clearer without
breaking the quine.

(The string itself has an embedded newline, which is presumably just for
USENET -- you have to remove it (so that the string is on a single line)
to make it work)

- jonathan
Nov 14 '05 #6
In article <gs*****************@newsread3.news.pas.earthlink. net>,
"Mike Wahler" <mk******@mkwahler.net> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:We*****************@newsread3.news.pas.earthl ink.net...

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).


Also note that the code can be easily rendered portable:

Replace those ASCII values with character literals.
( '\n' and '"' )


Except that that breaks the quine -- the output looks like:

char x[]="#include <stdio.h>%cchar x[]=%c%s%c;%cint main() {printf(x,'
','"',x,'"','
','
';return 0;}%c";
int main() {printf(x,'
','"',x,'"','
','
';return 0;}

You can't even use '\"' -- the string will no longer compile.

Cheers,
- jonathan
Nov 14 '05 #7
On Sat, 16 Oct 2004 19:47:04 GMT
"Robert Rotstein" <rr*******@verizon.net> wrote:
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, nor how the % formatting
characters work in this instance. Can someone explain just how this
works?


%c takes an integer and prints the character in the execution character
set that corresponds to that character, so for an ASCII implementation
the 10 corresponds to a new line and the 34 corresponds to a '"'. Since
it does not print out a carriage return (code 13 in ASCII) it won't even
work on all ASCII systems. On some you will get
#include <stdio.h>
char x[] ...
especially likely if piping stdout direct to a printer.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8
"Jonathan Adams" <jw*****@gmail.com> wrote in message
news:jw***************************@news1nwk.sfbay. sun.com...
In article <gs*****************@newsread3.news.pas.earthlink. net>,
"Mike Wahler" <mk******@mkwahler.net> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:We*****************@newsread3.news.pas.earthl ink.net...

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).


Also note that the code can be easily rendered portable:

Replace those ASCII values with character literals.
( '\n' and '"' )


Except that that breaks the quine -- the output looks like


a picture of me with a red face. :-)

-Mike
Nov 14 '05 #9
bd
Robert Rotstein wrote:
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, nor how the % formatting characters work in this
instance. Can someone explain just how this works?


%c means to take an integer, and print out the character corresponding to
it. Presumably the numbers are ASCII codes for various characters. Of
course, the Standard does not say that those codes mean the letters that
they do, but one could of course write a quine via different means.
Nov 14 '05 #10
"Robert Rotstein" <rr*******@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, nor how the % formatting characters work in this
instance. Can someone explain just how this works?


As others have said, they are hardcoded ASCII values for '\n' (10) and
'"' (34). As a result the wiki-program is not guaranteed to work because
the standard allows for different encodings. Below is a program that is
guaranteed to work. (Can it be improved (while keeping the lines short)?)

$ gcc -std=c99 -Wall -pedantic -O fixpoint.c
$ ./a.out
#include<stdio.h>
char*i="\\#include<stdio.h>",n='\n',q='"',*p=
"%s%cchar*i=%c%c%s%c,n='%cn',q='%c',*p=%c%c%s%c,*m =%c%c%s%c%c;%s%c",*m=
"int main(){return!printf(p,i+1,n,q,*i,i,q,*i,q,n,q,p,q ,n,q,m,q,n,m,n);}"
;int main(){return!printf(p,i+1,n,q,*i,i,q,*i,q,n,q,p,q ,n,q,m,q,n,m,n);}
$ ./a.out | diff - fixpoint.c
$
Daniel Vallstrom
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ollie | last post by:
I am trying to figure out how a complex program written in python works. I am new to python so looking at the source code directly is not helping me much. As such, I am running the program under...
1
by: Dan Stromberg | last post by:
The below small program is giving strange behavior. At the bottom of the code, please find "this works" and "this doesn't work" comments. Why does one work and the other not? TIA. ...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
10
by: An Ony | last post by:
Hi, I made a console program in C# and it works for me. Now I sent it to someone else and he can't run it. Program error or something. His OS is in Swedish. What could this be? Does he have to...
10
by: Vig | last post by:
What is a functio nthat produces itself called? I can;t recall for the life of me... Thanks Cheers! -- Vig
27
by: Neil | last post by:
Hello all! I wrote program with a array of pointers, and I suspect they are pointing at each other in the Do ...While loop. Something is messed up with the increment variable word. A program...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
39
by: mike3 | last post by:
Hi. I was writing a program in C++ that generates fractals. I got this weird bug though right now that's holding it up and was wondering if you could help. Anyway, it seems that when this...
334
by: Antoninus Twink | last post by:
The function below is from Richard HeathField's fgetline program. For some reason, it makes three passes through the string (a strlen(), a strcpy() then another pass to change dots) when two would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.