Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does i["string"] work as a condition statement?

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?

like image 532
Keshava GN Avatar asked Feb 28 '26 21:02

Keshava GN


1 Answers

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.

like image 161
ShadowRanger Avatar answered Mar 02 '26 11:03

ShadowRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!