By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Who can help me to interprete this code?

P: n/a
main(a)
{
printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}" ,34);
}
After executing it , it prints itself's code

Apr 13 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
wi************@gmail.com opined:
main(a)
{
printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}" ,34);
}


A good compiler could. Mine says (with pedantic all the way up):

testbed.c:2: warning: return type defaults to ‘int’
testbed.c: In function ‘main’:
testbed.c:3: warning: implicit declaration of function ‘printf’
testbed.c:3: warning: incompatible implicit declaration of built-in
function ‘printf’
testbed.c:3: error: assignment makes integer from pointer without
a cast
testbed.c:3: error: passing argument 1 of ‘printf’ makes pointer
from integer without a cast
testbed.c:3: warning: operation on ‘a’ may be undefined
testbed.c:4: warning: control reaches end of non-void function

As you can see, in the context of Standard C, there's nothing to
interpret. It doesn't even compile. Even if you ignore all the
warnings, it invokes Undefined Behaviour, and whatever result you get
cannot be trusted (esp. in the case you see demons flying out your
nose).

Just don't do it, and please don't try to learn from it.

--
"Nature abhors a Vacuum"

-- Brian Behlendorf on OSS (Open Sources, 1999 O'Reilly and
Associates)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 13 '06 #2

P: n/a
It's a Quine.

:quine: /kwi:n/ /n./ [from the name of the logician Willard van Orman
Quine, via Douglas Hofstadter] A program that generates a copy of its
own source text as its complete output.

don't know much about it.
but u can read from
http://www.nyx.net/~gthompso/quine.htm

Apr 13 '06 #3

P: n/a
wi************@gmail.com wrote:
main(a)
{
printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}" ,34);
}
After executing it , it prints itself's code


Perhaps, but it's not valid Standard C and even when compiled
successfully causes undefined behaviour.

Apr 13 '06 #4

P: n/a
I read it from the <Hacker's Delight>(wrote by Henry S.Warren Jr), and
then I compiled it successfully with the Dev-Cpp(Gcc) , So , I think
does the "main(a)" define the variable "a" as the "char * "
implicitly?(From the ANSI C "main(int argc , char* argv)")

Apr 13 '06 #5

P: n/a
a is the format, and a is "main(a){printf(a,34,a=%c%s%c,34);}"
So it is the same as

printf("main(a){printf(a,34,a=%c%s%c,34);}",34,"ma in(a){printf(a,34,a=%c%s%c,34);}",34);

then, of cause, the result will be
main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s% c,34);}",34);}

It's a trick, it not prints itself's code, it just print as the code.

Apr 13 '06 #6

P: n/a
All right , Is it relative to the interpretaion method of the function
of "printf"(Interpretate the expression from the right to the left) ?

Apr 13 '06 #7

P: n/a
Not special for printf. All the function does this. I'm not sure wether
it is c standard.

Apr 13 '06 #8

P: n/a
Kevin HZ said:
Not special for printf. All the function does this. I'm not sure wether
it is c standard.


It isn't. The order of evaluation of parameters is unspecified. Note that
the quine in question does not rely on a given order of evaluation.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 13 '06 #9

P: n/a

Richard Heathfield wrote:
Note that
the quine in question does not rely on a given order of evaluation.

?

In case of evaluation from left to rigth, first step is to evaluate
"a". This is an integer equal to the program number of arguments,
tipically 1. Taken it as pointer to the printf format, it will produce
a crash inside "printf"?

Apr 13 '06 #10

P: n/a
tmp123 said:
In case of evaluation from left to rigth, first step is to evaluate
"a".


Forget I said anything - I didn't look closely enough. Yeah, that quine is
screwed. There are better ones.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 13 '06 #11

P: n/a
wi************@gmail.com wrote:
All right , Is it relative to the interpretaion method of the
function of "printf"(Interpretate the expression from the right to
the left) ?


See below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 13 '06 #12

P: n/a
Kevin HZ wrote:
Not special for printf. All the function does this. I'm not sure
wether it is c standard.


See below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 13 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.