Who can help me to interprete this code?

W

willamette3597

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
 
V

Vladimir S. Oka

(e-mail address removed) 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>
 
J

JyotiC

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
 
S

santosh

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

willamette3597

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)")
 
K

Kevin HZ

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,"main(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.
 
W

willamette3597

All right , Is it relative to the interpretaion method of the function
of "printf"(Interpretate the expression from the right to the left) ?
 
K

Kevin HZ

Not special for printf. All the function does this. I'm not sure wether
it is c standard.
 
R

Richard Heathfield

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

tmp123

Richard said:
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"?
 
R

Richard Heathfield

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

Default User

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top