here is the smallest code that output itself. But can't grasp how this works. can somebody explain?
main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}
here is the smallest code that output itself. But can't grasp how this works. can somebody explain?
main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}
I bet it won't work on a 64-bit platform unless its model is ILP64 (64-bit int
s), because it relies on int
being big enough to contain a char*
.
It declares a variable a
that contains a copy of the code minus the string itself, and uses printf()'s formatting codes to output both the code and the string. Do you need more details?
These codes called as quine codes.The computer languages supports this features till a fixed point. As per wikipedia
a fixed point (sometimes shortened to fixpoint, also known as an invariant point) of a function is an element of the function's domain that is mapped to itself by the function
means means f(f(...f(c)...)) = fn(c) = c
where c is some constant
for example
f(x) = x^2 - 3 x + 4,
then 2 is a fixed point of f, because f(2) = 2
main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}
can be rewritten to
main(a) {
a = "main(a){printf(a,34,a=%c%s%c,34);}";
printf(a, 34, a, 34);
}
These two versions are not equivalent, but you could use the second version to understand what is going on in the first version.