Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

program that prints itself, how does it work?

Tags:

c

quine

I came across a program that prints itself on this site, i.e. it prints the program code.

The program code is:

#include <stdio.h>
char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%cprintf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c    return 0;%c}%c";
//what is this line doing, what is the use of %c and %s and what properties of %c and %s are being used here?
int main()
{
        printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
        //what is this print function doing, and how?
        return 0;
}

And the explanation given is:

The two key tricks here are using a string with an embedded %s specifier to allow the string to contain itself when printed, and to use the %c format specifier to allow printing out special characters like newlines, which could not otherwise be embedded in the output string.

I didn't understand how the program is working. I have mentioned the lines i need the explanation about, how they work and what are they doing. Please explain.

like image 930
SpeedBirdNine Avatar asked Oct 07 '11 17:10

SpeedBirdNine


People also ask

Can you write a program that prints itself?

Yes. A programme that can make a copy of itself is called a "quine". The basic idea of most quines is: You write code that takes a string literal s and prints it, while replacing occurrences (or the occurrence) of a special substring foo in s by the value of s itself.

How do quines work?

Introduction. A quine is a program which prints its own listing. This means that when the program is run, it must print out precisely those instructions which the programmer wrote as part of the program (including, of course, the instructions that do the printing, and the data used in the printing).

What creates copy of itself and execute it?

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".

What is a Polyquine?

A polyquine is both quine and polyglot. 1. You are to write a quine which is valid in at least two different languages. This is code golf, so the shortest answer (in bytes) wins.


2 Answers

char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%cprintf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c    return 0;%c}%c";

There is a char pointer name "program" which is used to store the string and %c and %s are format specifiers for char and string arguments respectively.

printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);

printf function is printing output to console, 10 here is ASCII code for NEWLINE and 34 for " printf parameters are doing

  • program , passing string to be printed
  • 10 , passing 10 ASCII code for first %c (will be converted to character newline)
  • program , passing same string again to %s in program to print same string again
  • 34 , passing 34 ASCII code for second %c (will be converted to character double qoutes)
  • 10 , passing 10 ASCII code for 3rd %c (will be converted to character newline)
  • 10 , passing 10 ASCII code for 4th %c (will be converted to character newline)
  • 10 , passing 10 ASCII code for 5th %c (will be converted to character newline)
  • 10 , passing 10 ASCII code for 6th %c (will be converted to character newline)
  • 10 , passing 10 ASCII code for 7th %c (will be converted to character newline)
  • 10 , passing 10 ASCII code for 8th %c (will be converted to character newline)
like image 114
ammar26 Avatar answered Oct 01 '22 10:10

ammar26


Printf prints the string given as the first argument (in this case the string in *program) substituting the other arguments where you have a %s or %c

%s means the arguement is a string, %c is a character.

As the note says, it uses %s to print a copy of the program string inside the program string - hence making a copy, and uses the %c to print the characters 10 (new line) and 34 "

like image 38
Martin Beckett Avatar answered Oct 01 '22 09:10

Martin Beckett