write a single line C program whose output is the program itself

Discussion in 'C Programming' started by Puneet, Mar 16, 2005.

  1. Puneet

    Puneet Guest

    Hi ALL,

    I have a silly question... (may be)

    Can we write a single line C program whose output is the program
    itself?

    Is anybody know the answer please tell me.

    Puneet
     
    Puneet, Mar 16, 2005
    #1
    1. Advertising

  2. Puneet

    bjrnove Guest

    The c standard doesn't say anything about how a program is in memory.
    So to make a program that does this on every system will probably not
    be possible.
    On some systems (like windows), if you compile your program without any
    optimisation everything usaly end up in memory almost as you write the
    program. This means that the following would print out it self on some
    systems:

    int main(void)
    {
    unsigned char *pCur = (unsigned char*)main, *pEnd = (unsigned
    char*)emptyfunc;
    while(pCur < pEnd) printf("%c", *pCur++);
    }

    void emptyfunc(void)
    {
    return;
    }

    This isn't something you usaly would want to do. It's much bether to
    get a disassembler.

    --
    bjrnove
     
    bjrnove, Mar 16, 2005
    #2
    1. Advertising

  3. Puneet

    bjrnove Guest

    I forgot to declare emtyfunc, so you will have to add
    void emptyfunc();
    before main. :)

    --
    bjrnove
     
    bjrnove, Mar 16, 2005
    #3
  4. Puneet said,on 3/16/2005 3:53 PM:
    > Hi ALL,
    >
    > I have a silly question... (may be)
    >
    > Can we write a single line C program whose output is the program
    > itself?
    >
    > Is anybody know the answer please tell me.
    >
    > Puneet
    >


    google for Quine...
    you'll get lots of such programs...

    --
    Shanmu.
     
    Shanmuhanathan T, Mar 16, 2005
    #4
  5. Puneet

    Roy Hills Guest

    On 16 Mar 2005 02:23:49 -0800, "Puneet" <>
    wrote:
    >Can we write a single line C program whose output is the program
    >itself?


    Ken Thompson mentions this in his classic paper "Reflections on
    Trusting Trust" (from Communication of the ACM, Vol. 27, No. 8, August
    1984, pp. 761-763).

    This paper is available from:

    http://www.acm.org/classics/sep95/

    If you've not already seen it, it's well worth a read.

    Roy
     
    Roy Hills, Mar 16, 2005
    #5
  6. In article <>,
    Roy Hills <> wrote:
    >On 16 Mar 2005 02:23:49 -0800, "Puneet" <>
    >wrote:
    >>Can we write a single line C program whose output is the program
    >>itself?

    >
    >Ken Thompson mentions this in his classic paper "Reflections on
    >Trusting Trust" (from Communication of the ACM, Vol. 27, No. 8, August
    >1984, pp. 761-763).


    Note that the one he presents won't fit on one line.

    But you can just take out the line breaks to get everything but the
    preprocessor directives on one line, and most implementations let you
    invoke the preprocessor separately to get a (nonportable) program that
    can have all the line breaks removed and still be valid...


    dave
    ('twill be a long line though)

    --
    Dave Vandervies
    So you consider it a lie to express an opinion that is supported by facts?
    Interesting, and presumably the reason why so many of your opinions are *not*
    supported by facts. --Richard Heathfield roasts a troll in comp.lang.c
     
    Dave Vandervies, Mar 16, 2005
    #6
  7. Puneet

    Guest

    Dave Vandervies wrote:
    > In article <>,
    > Roy Hills <> wrote:
    > >On 16 Mar 2005 02:23:49 -0800, "Puneet" <>
    > >wrote:
    > >>Can we write a single line C program whose output is the program
    > >>itself?

    > >
    > >Ken Thompson mentions this in his classic paper "Reflections on
    > >Trusting Trust" (from Communication of the ACM, Vol. 27, No. 8,

    August
    > >1984, pp. 761-763).

    >
    > Note that the one he presents won't fit on one line.
    >
    > But you can just take out the line breaks to get everything but the
    > preprocessor directives on one line, and most implementations let you
    > invoke the preprocessor separately to get a (nonportable) program

    that
    > can have all the line breaks removed and still be valid...
    >
    >
    > dave
    > ('twill be a long line though)


    Does the OP mean output the source of the program, or output the
    program though?
     
    , Mar 16, 2005
    #7
  8. On 16 Mar 2005 02:23:49 -0800, "Puneet" <> wrote:
    >Can we write a single line C program whose output is the program
    >itself?


    Some of we can. It's not proper C or portable. With 2 lines, it can be.

    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list
     
    Kevin D. Quitt, Mar 16, 2005
    #8
  9. [OT] Re: write a single line C program whose output is the program itself

    On Wed, 16 Mar 2005 wrote:
    >> Roy Hills <> wrote:
    >>> "Puneet" <> wrote:
    >>>> Can we write a single line C program whose output is the program
    >>>> itself?
    >>>
    >>> Ken Thompson mentions this in his classic paper "Reflections on
    >>> Trusting Trust" (from Communication of the ACM, Vol. 27, No. 8,
    >>> August 1984, pp. 761-763).


    > Does the OP mean output the source of the program, or output the
    > program though?


    If you think there's a quantitative difference, then you haven't
    been programming long enough.

    -Arthur
     
    Arthur J. O'Dwyer, Mar 16, 2005
    #9
  10. Kevin D. Quitt wrote:
    > On 16 Mar 2005 02:23:49 -0800, "Puneet" <>

    wrote:
    > >Can we write a single line C program whose output is the program
    > >itself?

    >
    > Some of we can. It's not proper C or portable. With 2 lines, it can

    be.

    $ gcc -std=c99 -Wall -W -pedantic -O fixpointOneLine.c
    $ ./a.out
    int printf(const char*s,...);char*p="\\int printf(const
    char*s,...);char*p=%c%c%s%c,n='%cn',q='%c',*m=%c%s%c;%s%c",n='\n',q='"',*m="int
    main(){return!printf(p+1,q,*p,p,q,*p,q,q,m,q,m,n);}";int
    main(){return!printf(p+1,q,*p,p,q,*p,q,q,m,q,m,n);}
    $ ./a.out | diff - fixpointOneLine.c
    $ wc fixpointOneLine.c
    1 7 245 fixpointOneLine.c


    Daniel Vallstrom
     
    Daniel Vallstrom, Mar 16, 2005
    #10
  11. Puneet

    Puneet Guest

    Hello All,

    Thanx for taking the question seriously.

    i clear the question is can we write a code which prints the program
    itself. means whatever code we written, in output it showz the whole
    program. But code should be in one line.

    Puneet
     
    Puneet, Mar 17, 2005
    #11
  12. On 17 Mar 2005 04:59:26 -0800, "Puneet" <> wrote:
    >Thanx for taking the question seriously.


    Right. Did you try compiling the gibberish he posted?

    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list
     
    Kevin D. Quitt, Mar 17, 2005
    #12
  13. On 17 Mar 2005 04:59:26 -0800, in comp.lang.c , "Puneet"
    <> wrote:

    >i clear the question is can we write a code which prints the program
    >itself. means whatever code we written, in output it showz the whole
    >program.


    Its called a quine and a websearch will find plenty.

    >But code should be in one line.


    thats easy. Linebreaks are irrelevant to C, they're just more whitespace.

    ps its spelt shows not showz


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 17, 2005
    #13
  14. Mark McIntyre wrote:
    > ...Linebreaks are irrelevant to C, they're just more whitespace.


    They are relevant to preprocessing in general, but particularly
    to directives.

    --
    Peter
     
    Peter Nilsson, Mar 18, 2005
    #14
  15. Re: [OT] Re: write a single line C program whose output is the program itself

    On Wed, 16 Mar 2005 15:00:57 -0500 (EST), in comp.lang.c , "Arthur J. O'Dwyer"
    <> wrote:

    >
    >On Wed, 16 Mar 2005 wrote:
    >>> Roy Hills <> wrote:
    >>> Does the OP mean output the source of the program, or output the

    >> program though?

    >
    > If you think there's a quantitative difference, then you haven't
    >been programming long enough.


    Hmm. There's no difference between outputting the source, and outputting the
    machine code.... ?


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 18, 2005
    #15
  16. Puneet wrote:
    > i clear the question is can we write a code which prints the program
    > itself. means whatever code we written, in output it showz the whole
    > program. But code should be in one line.


    The very post you replied to contains a one line quine which does what
    you want! Here is a repost of the program (meant to be on one line):

    int printf(const char*s,...);char*p="\\int printf(const
    char*s,...);char*p=%c%c%s%c,n='%cn',q='%c',*m=%c%s%c;%s%c",n='\n',q='"',*m="int
    main(){return!printf(p+1,q,*p,p,q,*p,q,q,m,q,m,n);}";int
    main(){return!printf(p+1,q,*p,p,q,*p,q,q,m,q,m,n);}


    Daniel Vallstrom
     
    Daniel Vallstrom, Mar 20, 2005
    #16
  17. Peter Nilsson wrote:
    > Mark McIntyre wrote:
    > > ...Linebreaks are irrelevant to C, they're just more whitespace.

    >
    > They are relevant to preprocessing in general, but particularly
    > to directives.


    More to the point they affect any solution to the problem at hand.

    Daniel Vallstrom
     
    Daniel Vallstrom, Mar 20, 2005
    #17
    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. Richard
    Replies:
    2
    Views:
    589
    informant
    Feb 10, 2004
  2. oyster
    Replies:
    2
    Views:
    286
    alex23
    Oct 21, 2008
  3. scad
    Replies:
    23
    Views:
    1,217
    Alf P. Steinbach
    May 17, 2009
  4. Aditya Mahajan
    Replies:
    4
    Views:
    108
    mortee
    Oct 15, 2007
  5. ela
    Replies:
    12
    Views:
    380
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page