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

P

Puneet

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
 
B

bjrnove

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.
 
B

bjrnove

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

Shanmuhanathan T

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...
 
R

Roy Hills

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
 
D

Dave Vandervies

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)
 
P

petermcmillan_uk

Dave said:
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?
 
A

Arthur J. O'Dwyer

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
 
D

Daniel Vallstrom

Kevin said:
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
 
P

Puneet

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
 
M

Mark McIntyre

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
 
P

Peter Nilsson

Mark said:
...Linebreaks are irrelevant to C, they're just more whitespace.

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

Mark McIntyre

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.... ?
 
D

Daniel Vallstrom

Puneet said:
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
 
D

Daniel Vallstrom

Peter said:
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
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top