473,789 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>%cch ar x[]=%c%s%c;%cint main()
{printf(x,10,34 ,x,34,10,10);re turn 0;}%c";
int main() {printf(x,10,34 ,x,34,10,10);re turn 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 1893

"Robert Rotstein" <rr*******@veri zon.net> wrote in message
news:YUecd.1533 $B34.281@trndny 02...
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>%cch ar x[]=%c%s%c;%cint main()
{printf(x,10,34 ,x,34,10,10);re turn 0;}%c";
int main() {printf(x,10,34 ,x,34,10,10);re turn 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>%cch ar x[]=%c%s%c;%cint main()
{printf(x,10,34 ,x,34,10,10);re turn 0;}%c";
int main() {printf(x,10,34 ,x,34,10,10);re turn 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******@mkwah ler.net> wrote in message
news:We******** *********@newsr ead3.news.pas.e arthlink.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.and rew.cmu.edu> wrote in message
news:Pi******** *************** ***********@uni x41.andrew.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*******@veri zon.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>%cch ar x[]=%c%s%c;%cint main()
{printf(x,10,34 ,x,34,10,10);re turn 0;}%c";
int main() {printf(x,10,34 ,x,34,10,10);re turn 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.earth link.net>,
"Mike Wahler" <mk******@mkwah ler.net> wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:We******** *********@newsr ead3.news.pas.e arthlink.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>%cch ar 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*******@veri zon.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>%cch ar x[]=%c%s%c;%cint main()
{printf(x,10,34 ,x,34,10,10);re turn 0;}%c";
int main() {printf(x,10,34 ,x,34,10,10);re turn 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.s fbay.sun.com...
In article <gs************ *****@newsread3 .news.pas.earth link.net>,
"Mike Wahler" <mk******@mkwah ler.net> wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:We******** *********@newsr ead3.news.pas.e arthlink.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>%cch ar x[]=%c%s%c;%cint main()
{printf(x,10,34 ,x,34,10,10);re turn 0;}%c";
int main() {printf(x,10,34 ,x,34,10,10);re turn 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

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

Similar topics

0
1281
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 IDLE so I can step through the code. The program runs fine when it is run by itself. However, when I try to run the program in IDLE, I run into problems were a module cannot be found or is unavailable. To work around this problem I tired to...
1
1398
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. #!/usr/bin/python import string
22
3610
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 Newsgroup Readers: If you circulate copies of this report to groups of computer programmers at different universities etc. around the world then they might find the subject matter to be interesting.
10
2495
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 have the .NET FrameWork installed to run ..NET programs? I sent it to him once as exe and another time as an ace, so I don't think it got messed up during transfer. Any thoughts?
10
1396
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
2181
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 clip of what I'm talking about. #include <stdio.h> #include <string.h>
41
2479
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 an exe, not from the Debug option. If I try to launch the debug on the crashing program, it'll close before the debugger opens. Any suggestions as to how I should proceed? Thanks in advance.
39
2025
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 code is executed, it corrupts the memory causing a crash later on (the program actually causes an "access violation" error in Windows, which the program is running under):
334
11613
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 clearly be sufficient. This could lead to unnecessarily bad performance on very long strings. It is also written in a hard-to-read and clunky style. char *dot_to_underscore(const char *s) { char *t = malloc(strlen(s) + 1); if(t != NULL)
0
9659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9504
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10134
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9977
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9011
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2903
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.