Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > write a single line C program whose output is the program itself

Reply
Thread Tools

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

 
 
Puneet
Guest
Posts: n/a
 
      03-16-2005
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

 
Reply With Quote
 
 
 
 
bjrnove
Guest
Posts: n/a
 
      03-16-2005
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

 
Reply With Quote
 
 
 
 
bjrnove
Guest
Posts: n/a
 
      03-16-2005
I forgot to declare emtyfunc, so you will have to add
void emptyfunc();
before main.

--
bjrnove

 
Reply With Quote
 
Shanmuhanathan T
Guest
Posts: n/a
 
      03-16-2005
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.
 
Reply With Quote
 
Roy Hills
Guest
Posts: n/a
 
      03-16-2005
On 16 Mar 2005 02:23:49 -0800, "Puneet" <(E-Mail Removed)>
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

 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      03-16-2005
In article <(E-Mail Removed)>,
Roy Hills <(E-Mail Removed)> wrote:
>On 16 Mar 2005 02:23:49 -0800, "Puneet" <(E-Mail Removed)>
>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 http://www.velocityreviews.com/forums/(E-Mail Removed)
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
 
Reply With Quote
 
petermcmillan_uk@yahoo.com
Guest
Posts: n/a
 
      03-16-2005

Dave Vandervies wrote:
> In article <(E-Mail Removed)>,
> Roy Hills <(E-Mail Removed)> wrote:
> >On 16 Mar 2005 02:23:49 -0800, "Puneet" <(E-Mail Removed)>
> >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?

 
Reply With Quote
 
Kevin D. Quitt
Guest
Posts: n/a
 
      03-16-2005
On 16 Mar 2005 02:23:49 -0800, "Puneet" <(E-Mail Removed)> 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
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      03-16-2005

On Wed, 16 Mar 2005 (E-Mail Removed) wrote:
>> Roy Hills <(E-Mail Removed)> wrote:
>>> "Puneet" <(E-Mail Removed)> 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
 
Reply With Quote
 
Daniel Vallstrom
Guest
Posts: n/a
 
      03-16-2005
Kevin D. Quitt wrote:
> On 16 Mar 2005 02:23:49 -0800, "Puneet" <(E-Mail Removed)>

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
resolve single line with multiple items into mutliple lines, single items ela Perl Misc 12 04-06-2009 06:47 PM
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
search for a python compiler program whose name is jingle oyster Python 2 10-21-2008 02:29 AM
Capturing the output of an external program line by line Aditya Mahajan Ruby 4 10-14-2007 11:28 PM



Advertisments