2

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);}
unwind
  • 391,730
  • 64
  • 469
  • 606
Unbound
  • 243
  • 2
  • 12

3 Answers3

1

I bet it won't work on a 64-bit platform unless its model is ILP64 (64-bit ints), 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?

Medinoc
  • 6,577
  • 20
  • 42
  • +1, but it works for most PC, `int` is big enough "in practice", for fun. – Aean Apr 02 '14 at 09:24
  • 1
    It's a (dangerous) shortcut for declaring the `a` local variable: Here it's declared as a parameter of main, with its type implicitly `int`. Declaring it as a true `char*` inside the function body would have been the right thing to do, but would have made the quine longer. – Medinoc Apr 02 '14 at 15:05
1

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
Ankur
  • 3,584
  • 1
  • 24
  • 32
0
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.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47