472,780 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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

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

Nov 14 '05 #1
16 9034
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

Nov 14 '05 #2
I forgot to declare emtyfunc, so you will have to add
void emptyfunc();
before main. :-)

--
bjrnove

Nov 14 '05 #3
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.
Nov 14 '05 #4
On 16 Mar 2005 02:23:49 -0800, "Puneet" <mr*************@gmail.com>
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

Nov 14 '05 #5
In article <ga********************************@4ax.com>,
Roy Hills <ro******@hotmail.com> wrote:
On 16 Mar 2005 02:23:49 -0800, "Puneet" <mr*************@gmail.com>
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 dj******@csclub.uwaterloo.ca
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
Nov 14 '05 #6

Dave Vandervies wrote:
In article <ga********************************@4ax.com>,
Roy Hills <ro******@hotmail.com> wrote:
On 16 Mar 2005 02:23:49 -0800, "Puneet" <mr*************@gmail.com>
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, August1984, 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?

Nov 14 '05 #7
On 16 Mar 2005 02:23:49 -0800, "Puneet" <mr*************@gmail.com> 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
Nov 14 '05 #8

On Wed, 16 Mar 2005 pe**************@yahoo.com wrote:
Roy Hills <ro******@hotmail.com> wrote:
"Puneet" <mr*************@gmail.com> 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
Nov 14 '05 #9
Kevin D. Quitt wrote:
On 16 Mar 2005 02:23:49 -0800, "Puneet" <mr*************@gmail.com>

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

Nov 14 '05 #10
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

Nov 14 '05 #11
On 17 Mar 2005 04:59:26 -0800, "Puneet" <mr*************@gmail.com> wrote:
Thanx for taking the question seriously.


Right. Did you try compiling the gibberish he posted?

--
#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
Nov 14 '05 #12
On 17 Mar 2005 04:59:26 -0800, in comp.lang.c , "Puneet"
<mr*************@gmail.com> wrote:
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
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #13
Mark McIntyre wrote:
...Linebreaks are irrelevant to C, they're just more whitespace.


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

--
Peter

Nov 14 '05 #14
On Wed, 16 Mar 2005 15:00:57 -0500 (EST), in comp.lang.c , "Arthur J. O'Dwyer"
<aj*@nospam.andrew.cmu.edu> wrote:

On Wed, 16 Mar 2005 pe**************@yahoo.com wrote:
Roy Hills <ro******@hotmail.com> wrote:
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.


Hmm. There's no difference between outputting the source, and outputting the
machine code.... ?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #15
Puneet wrote:
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

Nov 14 '05 #16
Peter Nilsson wrote:
Mark McIntyre wrote:
...Linebreaks are irrelevant to C, they're just more whitespace.


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

Nov 14 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
14
by: Xah Lee | last post by:
is there a way to condense the following loop into one line? # -*- coding: utf-8 -*- # python import re, os.path imgPaths= # change the image path to the full sized image, if it exists
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
5
by: OZ | last post by:
the serproxy claim itself a multi-thread proxy thing. I have sent email to write the original writer and there is no replay after 3 weeks. my configuration and setting are good. ...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
14
by: WUV999U | last post by:
Hi I am fairly familiar in C but not much. I want to know how I can write a html parser in C that only parses for the image file in the html file and display or print all the images found in...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
8
by: Mike Silva | last post by:
Hello all, I'm a longtime programmer (embedded with a smattering of desktop stuff as well) who knows very little about web programming. Right now I am developing the prototype of a multi-user...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.