views:

283

answers:

1

I am trying to execute following code which is the 1988 entry of Obfuscated C Code Contest.

#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
        _-_-_-_-_-_-_-_
            _-_-_-_
}

From entry description, this code is calculating pi by looking at its own area. I successfully compiled it without changing the code. But when I executed, it is giving me a value 0.25, what I am expecting is 3.14. Code description says it is in K&R C and it doesn't work correctly in ANSI C without some change. I think I have to do those modification to execute it properly. I don't have any previous experience with K&R C. So can someone help me to change above code to ANSI C or point to the problems if any. I am using Microsoft Visual Studio 2008 to execute this.

+6  A: 

If you have GCC, compile with the '-traditional-cpp' flag.

The difference is whether the '-_' sequence is translated to '- -F<00' or '--F<00'.

The one space is crucial: it's the difference between double negation and pre-decrement.

Jonathan Leffler
Unfortunately, the OP hid their compiler in the last sentence, when most of us (myself included until a couple seconds ago) stop reading. He/She is using VS2008.
Chris Lutz
Hats off, sir. I saw the convergence process (the program is not really working "off its own area", the iterations are just arranged in a cute fashion), but this detail in interpreting -_ wow!
mjv
@Chris: I saw the VS2008; I don't have it. I have my doubts that VS2008 will support the equivalent of -traditional-cpp. That was why I put the 'if' up front.
Jonathan Leffler