C/C++ program that prints its own source code as its output

后端 未结 8 2296
天命终不由人
天命终不由人 2020-11-29 08:25

Wikipedia says it\'s called a quine and someone gave the code below:

char*s=\"char*s=%c%s%c;main(){printf(s,34,s,34);}\";main(){printf(s,34,s,34);}
         


        
8条回答
  •  庸人自扰
    2020-11-29 09:30

    A quine has some depth roots in fixed point semantics related to programming languages and to executions in general. They have some importance related to theoretical computer science but in practice they have no purpose.

    They are a sort of challenge or tricks.

    The literal requirement is just you said, literal: you have a program, its execution produces itself as the output. Nothing more nor less, that's why it's considered a fixed point: the execution of the program through the language semantics has itself as its ouput.

    So if you express the computation as a function you'll have that

    f(program, environment) = program
    

    In the case of a quine the environment is considered empty (you don't have anything as input neither precomputed before)

提交回复
热议问题