3

Check this obfuscated code in IOCCC. I'm trying to understand this.

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);}

Means:

int i;
main()
{
    for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hello, world!\n",'/'/'/'));
}
read(j,i,p)
{
    write(j/p+p,i---j,i/i);
}

Please explain me this : i["]<i;++i){--i;}"]

How this works as a condition statement here?

M.M
  • 138,810
  • 21
  • 208
  • 365
Keshava GN
  • 4,195
  • 2
  • 36
  • 47

1 Answers1

6

C indexing is kinda funky. a[b] is roughly equivalent to *(a+b) (with a degrading to a pointer to the first element). But addition is commutative, so a[b] and b[a] do the same thing. Thus:

i["]<i;++i){--i;}"]

is really just:

"]<i;++i){--i;}"[i]

but the reordering obfuscates by making it look like a normalish for loop if you miss the quotes. Since all values in the string are non-zero (and therefore "true") except for the NUL terminator, the loop would end when i was equal to the length of that string literal.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • @RichardPennington: Well, initially. `i` is changing value during execution. So the first check, when `i` is `0`, sure, it's `]`, but later checks `i` is incrementing. Doesn't really matter what it happens to produce; all that matters is zero or non-zero, and all but the `NUL` terminator of a string is non-zero. – ShadowRanger Dec 16 '15 at 04:22