473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Who can help me to interprete this code?

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
12 1565
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
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
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
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
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
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
Not special for printf. All the function does this. I'm not sure wether
it is c standard.

Apr 13 '06 #8
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: Edward King | last post by:
Hi! I am trying to achieve the following: I have a number of help pages (in the format help_nn.php where nn=helpid). I want to be able to open a particular help page by calling the function...
3
by: Ruben van Engelenburg | last post by:
Hi all, I'm running PHP 4.3.9 on IIS6/Win2003. What I'm trying to accomplish is that IIS interpretes the output of a CGI program (.exe) as PHP. Right now it just outputs the php code. Is this...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
6
by: frankh | last post by:
Got earlier very valuable feedback from this group, so I am trying again. Given a string with an arithmetic expression containing variables, e.g. "a+b*sin(x)", how can I calculate its value...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
4
by: anthonette50 | last post by:
I am trying to understand what the code below is doing, i know the functions of a string buffer, however am finding it difficult to interprete the aim of the code below. Hope someone can help me? ...
14
by: morebeer | last post by:
I got the same problem, hundreds of SQL tables been infected with this malicious javascript code. But although closing the original injection leak and also having replaced all strings in all...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.