473,802 Members | 2,400 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
10 1897
"Robert Rotstein" <rr*******@veri zon.net> wrote in message news:<YUecd.153 3$B34.281@trndn y02>...
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?


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="\\#incl ude<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!p rintf(p,i+1,n,q ,*i,i,q,*i,q,n, q,p,q,n,q,m,q,n ,m,n);}"
;int main(){return!p rintf(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
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
1399
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
3613
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
2496
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
1397
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
2182
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
2481
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
2029
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
11624
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
9562
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
10535
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10303
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...
0
10061
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
9111
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...
0
6838
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();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
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.