Thread: program to print itself

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    program to print itself

    guys i have a small doubt.....

    Is it possible to write a program which when executed prints itself as the output...
    i.e, the output should be the program itself.....

    the problem is using of files is not allowed.....

    i have no idea how to start without files....

    waiting for your ideas

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There was a post on the forum about 2 months back that did this. It was based on the program itself containing a copy of itself in hex in a char array - so you need to write a little preprocessor to take the file and generate a string of hex bytes, and then print those bytes twice in different ways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web for "quine".
    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

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    can you please direct me to that post....
    or give me the link or subject name if possible.......

    well it would be better if you can tell me the thing of preprocessor here.....

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sure you are just as able as me to search the forum - and you are the one wanting to do it, so why should I perform the search.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why don't you do as laserlight suggested? google.ca/search?q=quine
    How difficult is it to do a google search?

    You can find myriad examples here: http://www.nyx.net/~gthompso/self_c.txt
    Most of them aren't very legible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    thanxx got it!!!!!!!!!!!!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is it that my program does not print for case 1?
    By Jasper in forum C Programming
    Replies: 10
    Last Post: 07-02-2009, 12:57 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM