473,320 Members | 2,024 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.

How does C parse it?

I occur a oddity C program,when I surfed the Internet. I have
confussed by the style. Who can show detail information about it?
<code> (original)
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
</code>

And I cut down some of it.And the code is like following.
<code> (modified)
int i;

read(i)
{
write(1,i--,i/i);
}

main()
{
for(; i["]<i;++i){--i;}"]; read(i+++"hello, world!\n"))
;
}
</code>
And now my problem is that
1.How does for statement check the condition?
2.Why the variable "i" can use like array at the first position in the
second expression (In the for loops)? Directly, what does the string--
i["]<i;++i){--i;}"]; -- mean?
3.How does the read function pass argument?
The following information maybe have some help:
1.The assembly code generates by the gcc -s
<code>
.file "Test.c"
.text
..globl _read
.def _read; .scl 2; .type 32; .endef
_read:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl 8(%ebp), %eax
cltd
idivl 8(%ebp)
movl %eax, 8(%esp)
movl 8(%ebp), %eax
movl %eax, 4(%esp)
decl 8(%ebp)
movl $1, (%esp)
call _write
leave
ret
.def ___main; .scl 2; .type 32; .endef
LC0:
.ascii "]<i;++i){--i;}\0"
LC1:
.ascii "hello, world!\12\0"
..globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
L3:
movl _i, %eax
addl $LC0, %eax
cmpb $0, (%eax)
jne L5
jmp L4
L5:
movl _i, %eax
addl $LC1, %eax
movl %eax, (%esp)
incl _i
call _read
jmp L3
L4:
leave
ret
.comm _i, 16 # 4
.def _write; .scl 2; .type 32; .endef
</code>

Nov 14 '05 #1
6 1311
In the second question, As the assembly code showes ,
does the string ----- "]<i;++i){--i;}\0" ---- just take
a position ?
But the question is that how does the variable can use as array?

Nov 14 '05 #2
li************@163.com wrote:
I occur a oddity C program,when I surfed the Internet. I have
confussed by the style. Who can show detail information about it?
<code> (original)
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
</code>

And I cut down some of it.And the code is like following.
<code> (modified)
int i;

read(i)
{
write(1,i--,i/i);
}

main()
{
for(; i["]<i;++i){--i;}"]; read(i+++"hello, world!\n"))
;
}
</code>
And now my problem is that
1.How does for statement check the condition?
2.Why the variable "i" can use like array at the first position in the
second expression (In the for loops)? Directly, what does the string--
i["]<i;++i){--i;}"]; -- mean?
3.How does the read function pass argument?

Great program ,huh? Perhaps you'd enjoy it more if you figured it out
yourself. Still, here are some spoilers.

1) A for loop repeats until the middle condition evaluates to 0. (what
do C strings end with?)
2) In C, Array[i] is equiv to i[Array], since the compiler replaces both
with *(Array + i). So, with, say,
const char * string = "]<i;++i){--i;}";
/* string[i] is equiv to i[string] */
3) i is declared at file scope so is intialised to 0. (string literal +
integer) gives the address of the string plus a number, i.e. a pointer
into the string, which is passed to 'read', then i is incremented.
4) i-- in 'read' acts only on the local i, so is unimportant.

Cheers,
Rob M
Nov 14 '05 #3
li************@163.com wrote:
In the second question, As the assembly code showes ,
does the string ----- "]<i;++i){--i;}\0" ---- just take
a position ?
But the question is that how does the variable can use as array?


It isn't.

The expression `x[y]` means `*(x+y)`. The expressions `x+y` and `y+x`
have (almost [1]) the same meaning - `+` is commutative. So the expression
`i["s"]` means `*(i + "s")` means the same as `*("s" + i)` is meant by
`"s"[i]`; it's just array indexing.

[1] order-of-evaluation issues.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #4
Oh, thank you.
I look through you explain, and look again the assembly code.
It takes me suddenly enlighten.

Nov 14 '05 #5
Rob Morris wrote:
<code> (original)
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
</code>
...

Great program ,huh? Perhaps you'd enjoy it more if you figured it out
yourself. Still, here are some spoilers.


It is great. But it would be greater if the author made it just a little
bit longer and provided a declaration for the second parameter of 'read'
function ('i'). Without it the program produces undefined behavior,
which diminishes its value as an IOCCC entry. However, AFAIK it was one
of the first IOCCC submissions (if not the first) and for this reason
that little problem can be overlooked.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6
In article <d1**********@malatesta.hpl.hp.com>,
Chris Dollin <ke**@hpl.hp.com> wrote:
The expressions `x+y` and `y+x`
have (almost [1]) the same meaning - `+` is commutative. [1] order-of-evaluation issues.


It's worth noting that the order of evaluation of the operands isn't
specified, so if the order of evaluation affects anything important the
code is wrong anyways.

If both have side effects, though, the same implementation will be more
likely to consistently evaluate "left operand" or "right operand" first
than to consistently evaluate "x" or "y" first.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
You know, you should never say something like "No one is expecting..." on
Usenet. It is just too easy to disprove. The preferred form is "No one in his
right mind is expecting..." --Stephan H.M.J. Houben in comp.lang.scheme
Nov 14 '05 #7

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

Similar topics

2
by: Greg Kuperberg | last post by:
I plan to use pydoc for my Python project. After looking through the standard documentation, I am not sure how pydoc interprets its input. In its basic operation it evidently looks at the first...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
2
by: Norm via DotNetMonster.com | last post by:
How do you get a view to update when a table it uses is updated? I am using asp.net, C# and MS SQL. Thanks in advance - Norm -- Message posted via http://www.dotnetmonster.com
7
by: GS | last post by:
Hello, I'm receiving The state information is invalid for this page and might be corrupted error message when running code below. This happens on second post back. Why is it happening? My...
4
by: fyleow | last post by:
I create a new Python file with the following using Wing IDE. import feedparser d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml") print d.feed.title I get this error when...
5
by: js | last post by:
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss". I need to parse the text using System.DateTime.Parse() function with custom format. I got an error using the following code. ...
2
by: sree reddy | last post by:
..cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
11
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.