Output as SOURCE CODE itself !!

D

Dev

Hello Folks,

I need a "C" program, whose output display its SOURCE CODE
itself..

How can we do that? Please post solutions along with code
example (if possible)

Thanx..
 
R

Richard Heathfield

Dev said:
Hello Folks,

I need a "C" program, whose output display its SOURCE CODE
itself..

How can we do that? Please post solutions along with code
example (if possible)

/* save this file as foo.c */
#include <stdio.h>

int main(void)
{
FILE *fp = fopen("foo.c", "r");
if(fp != NULL)
{
int ch;
while((ch = getc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
}
return 0;
}

Alternatively, look up "quine" on the Web.
 
R

Richard Heathfield

Sunil Varma said:

Each has advantages and disadvantages.

__FILE__ will typically be replaced by a full path spec for the file, which
means you can run the program from anywhere on that system and still have
it pick up the file from that path - but if you move the program to a
different but compatible system, you must then reconstruct that path on the
new system and place the source file there, whereas my way you need only
dump the source file in any old place and then run the program from that
directory. (Yes, all these references to paths and directories make this
moderately off-topic.)
 
R

Richard Heathfield

Dev said:
Thanks Richard, that's what i thought, with file handlings we can do
this..

Now that you've learned that, go look up "quine" on the Web.
 
R

Richard Heathfield

Dev said:
Sorry, Richar, could u tell me what do u mean by "quine"

It's something you look up on the Web. Consider the possibilities inherent
in a Web search engine such as Google or Altavista. Consider also the fact
that I would not have suggested that you look up the word "quine" if it
were not relevant to your question.
 
J

John Devereux

Richard Heathfield said:
Dev said:


It's something you look up on the Web. Consider the possibilities inherent
in a Web search engine such as Google or Altavista. Consider also the fact
that I would not have suggested that you look up the word "quine" if it
were not relevant to your question.

He may be confused by the fact that the top site listed appears to be
for gay, black, disabled, Scottish women... :)
 
J

Jirka Klaue

John Devereux:
Richard Heathfield:

He may be confused by the fact that the top site listed appears to be
for gay, black, disabled, Scottish women... :)

Adding "source" to the search helps a lot.

Jirka
 
K

Keith Thompson

Dev said:
Hello Keith,
Thank for giving me link, i will follow the "Reply" format, sorry for
previous post, thanks once again..

Excellent, thank you.

One more thing: it's rarely necessary to quote the *entire* article to
which you're replying. You should delete anything that's not relevant
to your followup, while making sure that the attribution lines for
anythink you *do* quote are still there. Don't quote signatures
unless you're actually commenting on them.

Most posters here get this mostly right, so following their example is
a good approach.
 
M

Mark McIntyre

He may be confused by the fact that the top site listed appears to be
for gay, black, disabled, Scottish women... :)

Sure, but the /third/ entry looks kinda relevant (smiley noted...)
Mark McIntyre
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top