How does C parse it?

L

linzhenhua1205

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>
 
L

linzhenhua1205

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

Rob Morris

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 is equiv to i[Array], since the compiler replaces both
with *(Array + i). So, with, say,
const char * string = "]<i;++i){--i;}";
/* string 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
 
C

Chris Dollin

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"`; it's just array indexing.

[1] order-of-evaluation issues.
 
L

linzhenhua1205

Oh, thank you.
I look through you explain, and look again the assembly code.
It takes me suddenly enlighten.
 
A

Andrey Tarasevich

Rob said:
<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.
 
D

Dave Vandervies

Chris Dollin said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top