Thread: How to make a program that prints it's own source code???

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    How to make a program that prints it's own source code???

    Hello,
    I know 1 way of making a program which prints its own source code! And that is, first i save the source code and open that file only and print it
    But i dont want that. What i want is without saving the source code it should print its own source code! Is it possible?and if yes then how?please tell me??

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Not possible. Source code is not stored in the compiled executable.

    You might be able to do it with some kind of pseudo-macro, but that would be compiler dependent (and ugly).

    Why would you want to do it anyways?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Taylors Falls, Minnesota
    Posts
    5
    Im kind of a noob but would it not be possible to store the code using File I\O and then print it

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What i want is without saving the source code it should print its own source code! Is it possible?and if yes then how?please tell me??
    Yes, it is possible. Search the web for quine.

    Why would you want to do it anyways?
    Fun, laughter, peace and joy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    hahahahahueheh laserlight! Yes why would anybody want to do it?you're right!
    But this is an interview question. When we go for interview we can't ask the interviewer this question! like why would you want to do it?for fun?peace?laughter and joy?can we??

    http://www.techinterviews.com/?p=317

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by chottachatri View Post
    hahahahahueheh laserlight! Yes why would anybody want to do it?you're right!
    But this is an interview question. When we go for interview we can't ask the interviewer this question! like why would you want to do it?for fun?peace?laughter and joy?can we??

    http://www.techinterviews.com/?p=317
    If someone asked such a stupid question during an interview, first I'd ask them why they'd want to do that, then I'd wonder if it's the right place for me to work.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If someone asked such a stupid question during an interview
    The question is not so stupid. Everyone with the "classical" programming education is aware of such program. So if the person learns about it during interview it can indicate possible "shortcuts" in his education curve... And this can be futher investigated with other questions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    vart! yeah it's a classical programming question. Hey please tell me how to do it?is it possible or not?since the source code that we are making is residing in memory only i believe there has to be a way out. If we can know at which memory location the source code is currently running we can assign it's address to some character pointer variable..it's just a guess please if you know then tell me

    i know 1 method as i said but that's a simple file reading and printing on console. But i dont want that the source code should be on hard disk...it should be in memory only and be displayed on console..

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, I told you to search for quine, apparently you did, and you even posted a link to a page that has a solution. What more do you want?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    but laserlight what does this mean??

    Code:
    char*f="char*f=%c%s%c;main()
    {printf(f,34,f,34,10);}%c";
    main(){printf(f,34,f,34,10);}

    this solution simply copies the whole source code that is written in main again in some pointer variable and prints it! whats the sense of doing that?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    whats the sense of doing that?
    To solve the problem. It requires a little tweaking to be standard C++ code, of course.

    since the source code that we are making is residing in memory
    It is not residing in memory, unless you read from the source file, which you do not want to do. You forgot that the source code is compiled into object code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    So now will you tell me how to do that?or like that day you will start giving answer in round about manner??

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So now will you tell me how to do that?
    As in tweak the example to be standard C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    And i know the source code is compiled into object code. But will you please tell me how will the object code be build??if we are making the source code for the 1st time.That means during compilation there's no .obj file on hard disk!i hope you getting my point??the object file that doesn't exist at all..can we assign it's address to some file pointer??

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    It will do if it's not following the rules. I just want to do the job of printing the source code..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Replies: 1
    Last Post: 03-12-2002, 06:31 AM
  4. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM