[bitlug] Re: Interesting problem - revisited

  • From: Turuvanur Pavan-A20404 <pavan.tc@xxxxxxxxxxxx>
  • To: bitlug@xxxxxxxxxxxxx
  • Date: Fri, 28 May 2004 11:11:22 +0530

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

> I'll be posting the solution on friday. Can you make give it by then?

Ok, today is friday (as on 28th May 2004), and here is the solution :

First of all, if you properly 'indent' the code, this is what it will look like 

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

Things should be clear already.

This program exploits two of C's weird syntax. One is that 1[a] and a[1] is the 
For example,
1["somestring"] is same as "somestring"[1] which is 'o'.

Similarly, in the above program, the variable i (global, hence init to 0) 
indexes the 
string "]<i;++i){--i;}", which is null terminated. Hence when i indexes the nul 
character, the loop breaks. 

following that is the call to read() which is defined in the program and the 
arguments to it are '-' - '-', which is 0 ('-' minus '-'), pointer to "hello, 
world!\n" indexed by i again.
'/' / '/' is 1, since it is "something divided by the samething". 

hence, the call to read is resolved to read(0,ptr_to_hello[i],1);

Now, looking at the definition of the function read, it just writes to file 
descriptor represented by (j/p + p), which is (0/1 + 1), which is 1, which is 
nothing but STDOUT.

And hence the output :

$hello, world!


Other related posts: