Who can help me to interprete this code?

Discussion in 'C Programming' started by willamette3597@gmail.com, Apr 13, 2006.

  1. Guest

    main(a)
    {
    printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);
    }
    After executing it , it prints itself's code
    , Apr 13, 2006
    #1
    1. Advertising

  2. opined:

    > main(a)
    > {
    > printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);
    > }


    A good compiler could. Mine says (with pedantic all the way up):

    testbed.c:2: warning: return type defaults to ‘int’
    testbed.c: In function ‘main’:
    testbed.c:3: warning: implicit declaration of function ‘printf’
    testbed.c:3: warning: incompatible implicit declaration of built-in
    function ‘printf’
    testbed.c:3: error: assignment makes integer from pointer without
    a cast
    testbed.c:3: error: passing argument 1 of ‘printf’ makes pointer
    from integer without a cast
    testbed.c:3: warning: operation on ‘a’ may be undefined
    testbed.c:4: warning: control reaches end of non-void function

    As you can see, in the context of Standard C, there's nothing to
    interpret. It doesn't even compile. Even if you ignore all the
    warnings, it invokes Undefined Behaviour, and whatever result you get
    cannot be trusted (esp. in the case you see demons flying out your
    nose).

    Just don't do it, and please don't try to learn from it.

    --
    "Nature abhors a Vacuum"

    -- Brian Behlendorf on OSS (Open Sources, 1999 O'Reilly and
    Associates)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir S. Oka, Apr 13, 2006
    #2
    1. Advertising

  3. JyotiC Guest

    It's a Quine.

    :quine: /kwi:n/ /n./ [from the name of the logician Willard van Orman
    Quine, via Douglas Hofstadter] A program that generates a copy of its
    own source text as its complete output.

    don't know much about it.
    but u can read from
    http://www.nyx.net/~gthompso/quine.htm
    JyotiC, Apr 13, 2006
    #3
  4. santosh Guest

    wrote:
    > main(a)
    > {
    > printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);
    > }
    > After executing it , it prints itself's code


    Perhaps, but it's not valid Standard C and even when compiled
    successfully causes undefined behaviour.
    santosh, Apr 13, 2006
    #4
  5. Guest

    I read it from the <Hacker's Delight>(wrote by Henry S.Warren Jr), and
    then I compiled it successfully with the Dev-Cpp(Gcc) , So , I think
    does the "main(a)" define the variable "a" as the "char * "
    implicitly?(From the ANSI C "main(int argc , char* argv)")
    , Apr 13, 2006
    #5
  6. Kevin HZ Guest

    a is the format, and a is "main(a){printf(a,34,a=%c%s%c,34);}"
    So it is the same as

    printf("main(a){printf(a,34,a=%c%s%c,34);}",34,"main(a){printf(a,34,a=%c%s%c,34);}",34);

    then, of cause, the result will be
    main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}

    It's a trick, it not prints itself's code, it just print as the code.
    Kevin HZ, Apr 13, 2006
    #6
  7. Guest

    All right , Is it relative to the interpretaion method of the function
    of "printf"(Interpretate the expression from the right to the left) ?
    , Apr 13, 2006
    #7
  8. Kevin HZ Guest

    Not special for printf. All the function does this. I'm not sure wether
    it is c standard.
    Kevin HZ, Apr 13, 2006
    #8
  9. Kevin HZ said:

    > Not special for printf. All the function does this. I'm not sure wether
    > it is c standard.


    It isn't. The order of evaluation of parameters is unspecified. Note that
    the quine in question does not rely on a given order of evaluation.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Apr 13, 2006
    #9
  10. tmp123 Guest

    Richard Heathfield wrote:
    > Note that
    > the quine in question does not rely on a given order of evaluation.
    >



    ?

    In case of evaluation from left to rigth, first step is to evaluate
    "a". This is an integer equal to the program number of arguments,
    tipically 1. Taken it as pointer to the printf format, it will produce
    a crash inside "printf"?
    tmp123, Apr 13, 2006
    #10
  11. tmp123 said:

    > In case of evaluation from left to rigth, first step is to evaluate
    > "a".


    Forget I said anything - I didn't look closely enough. Yeah, that quine is
    screwed. There are better ones.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Apr 13, 2006
    #11
  12. Default User Guest

    wrote:

    > All right , Is it relative to the interpretaion method of the
    > function of "printf"(Interpretate the expression from the right to
    > the left) ?


    See below.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Apr 13, 2006
    #12
  13. Default User Guest

    Kevin HZ wrote:

    > Not special for printf. All the function does this. I'm not sure
    > wether it is c standard.


    See below.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Apr 13, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,152
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Randall Parker
    Replies:
    2
    Views:
    476
    intrader
    Nov 1, 2005
  3. keithb
    Replies:
    1
    Views:
    882
    Bruce Barker
    Mar 29, 2006
  4. TR
    Replies:
    1
    Views:
    649
    jbug-munich
    Jul 13, 2006
  5. Frank Miller
    Replies:
    4
    Views:
    313
    Richard Bos
    Jun 11, 2009
Loading...

Share This Page