Daniele Olmisani - daniele.olmisani@gmail.com, see LICENSE file.
main(p){printf(p="main(p){printf(p=%c%s%1$c,34,p);}",34,p);}
by Dario Dariol
A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output. Wikipedia
Compile using:
$ g++ -std=c++17 quine.cpp -o quine
Verify using:
$ ./quine | diff quine.cpp -
Please, refer to quine.cpp:
auto s = R"(
#include <iostream>
main() {
std::cout << "auto s = R\"(" << s << ")\";" << s;
}
)";
#include <iostream>
main() {
std::cout << "auto s = R\"(" << s << ")\";" << s;
}
Please, refer to quine3.cpp:
#include<cstdio>
int main(){char n[]=R"(#include<cstdio>
int main(){char n[]=R"(%s%c";printf(n,n,41);})";printf(n,n,41);}
Please, refer to quine2.cpp:
#include <iostream>
#include <string>
std::string s = R"(
#include <iostream>
#include <string>
std::string s = R"(
";
int main() {
std::cout << s.replace(59, 1, s+')');
return 0;
}
)";
int main() {
std::cout << s.replace(59, 1, s+')');
return 0;
}
Please, refer to quine1.cpp:
#include <iostream>
#include <iomanip>
int main() {
const char *s[] = {
" ",
"#include <iostream>",
"#include <iomanip>",
"",
"int main() {",
" const char *s[] = {",
" };",
" for(int i = 1; i < 6; ++i)",
" std::cout << s[i] << std::endl;",
" for(int i = 0; i < 15; ++i)",
" std::cout << s[0] << std::quoted(s[i]) << ',' << std::endl;",
" for(int i = 6; i < 15; ++i)",
" std::cout << s[i] << std::endl;",
" return 0;",
"}",
};
for(int i = 1; i < 6; ++i)
std::cout << s[i] << std::endl;
for(int i = 0; i < 15; ++i)
std::cout << s[0] << std::quoted(s[i]) << ',' << std::endl;
for(int i = 6; i < 15; ++i)
std::cout << s[i] << std::endl;
return 0;
}
Other interesting quines.
fn main(){print!("{}{0:?})}}","fn main(){print!(\"{}{0:?})}}\",")}
_='_=%r;print(_%%_)';print(_%_)
In Python the expression "name: %s" % value
performs a string formatting similar to the C printf function. The %r formatter addes single quotes around the value and the double %% sign is an escaping sequence of the character %.
A trivial quine in APL is given by any expression:
0
that will be output printed as result.
Instead, an interisting and non-trivial quine is
1⌽,⍨9⍴'''1⌽,⍨9⍴'''
Try the code in TryAPL
Let's we will trying to understand.
APL expressions are evaluated right-to-left, so starting from the end
'''1⌽,⍨9⍴'''
is evaluated as
'1⌽,⍨9⍴'
because ''' is an escaped single quote.
then
9⍴'''1⌽,⍨9⍴'''
is evaluated as
'1⌽,⍨9⍴''
because 9⍴ will add a new element at the end of the 8 charater length string (repeting the string as needed).
then
,⍨9⍴'''1⌽,⍨9⍴'''
is evaluated as
'1⌽,⍨9⍴'''1⌽,⍨9⍴''
because ,⍨ will append its argument to itself (comma is the append operator, while ⍨ will duplicate the right argument as left argument).
then
1⌽,⍨9⍴'''1⌽,⍨9⍴'''
is evaluated as
1⌽,⍨9⍴'''1⌽,⍨9⍴'''
because 1⌽ will rotate the right argument of one position (left argument).
Classic cheat.
1 LIST
A quine in CBM BASIC v2 that should be edited directly in a real Commodore 64.
Bit boring, but an actual quine.
1 READA$:PRINTA$CHR$(34)A$:DATA"1 READA$:PRINTA$CHR$(34)A$:DATA
Without any trick (just a little trouble in entering the last character, just go cursor back on the first char and press RETURN):
1 READA$:PRINTA$CHR$(34)A$CHR$(34):DATA"
1 READA$:PRINTA$CHR$(34)A$CHR$(34):DATA"
A quine using natural language.