Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Who can help me to interprete this code?

Reply
Thread Tools

Who can help me to interprete this code?

 
 
willamette3597@gmail.com
Guest
Posts: n/a
 
      04-13-2006
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

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      04-13-2006
http://www.velocityreviews.com/forums/(E-Mail 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>

 
Reply With Quote
 
 
 
 
JyotiC
Guest
Posts: n/a
 
      04-13-2006
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

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-13-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
willamette3597@gmail.com
Guest
Posts: n/a
 
      04-13-2006
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)")

 
Reply With Quote
 
Kevin HZ
Guest
Posts: n/a
 
      04-13-2006
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.

 
Reply With Quote
 
willamette3597@gmail.com
Guest
Posts: n/a
 
      04-13-2006
All right , Is it relative to the interpretaion method of the function
of "printf"(Interpretate the expression from the right to the left) ?

 
Reply With Quote
 
Kevin HZ
Guest
Posts: n/a
 
      04-13-2006
Not special for printf. All the function does this. I'm not sure wether
it is c standard.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-13-2006
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)
 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      04-13-2006

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"?

 
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
Tool/Editor to interprete #if(Switch == ValueA) available ? Frank Miller C Programming 4 06-11-2009 07:45 PM
Where can I find the help I need ? Can you help ? penang@catholic.org Computer Support 8 06-09-2007 02:55 PM
JBossWS Server Can't Interprete PHP Array TR Java 1 07-13-2006 09:35 AM
ReQ; Help having problem with cisco router 1602R the Lan part works cannot log on to configure so PC can access internet, router connects to T1 line thier is an alarm light on anyone can help smokin@aol.com Computer Support 4 10-30-2004 06:06 AM
Nothing can be found... HELP HELP HELP ... -=zZz=- Computer Support 15 02-29-2004 10:47 PM



Advertisments