6

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
Vadakkumpadath
  • 1,445
  • 13
  • 21
  • 7
    For the love of $DEITY, why? I can't see this as being the least bit useful. The obfuscated competitions are fine as a way of showing how "clever" you can be, but they really have little relevance in the real world. My advice, find a real problem and solve it. – paxdiablo Oct 09 '09 at 06:01
  • 1
    @Pax: +Inf for $DEITY :) – Jacob Oct 09 '09 at 06:03
  • 1
    @Pax: -Inf for infinite blasphemy :) – DVK Oct 09 '09 at 06:12
  • 20
    So, since when has 'fun' stopped being an acceptable excuse for doing something? I think it makes a nice change from the "do my job for me" questions of other posters. – Michiel Buddingh Oct 09 '09 at 06:13
  • 2
    @Pax: Who among us has not been called upon to decipher some horrifying , yet somewhat functional, mess of "cleverness" left behind by a predecessor? Studying intentionally obfuscated code seems like a prudent form of self-defense -- even more so if it's *genuinely* clever! – Jim Lewis Oct 09 '09 at 06:31
  • @Pax: Personally I'd prefer $DEITIES, but what the heck... Nontheless I agree with Michiel: 'fun' is on of the greatesd excuses to do something... – Nils Oct 09 '09 at 06:35
  • 1
    I knew I remembered this problem from before: http://stackoverflow.com/questions/841646/is-define-supposed-to-add-spaces-around-macros – Michael Burr Oct 09 '09 at 06:38
  • Removed C++ tag, as the question specifically relates to C. – Puppy Mar 15 '11 at 20:27

2 Answers2

10

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
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    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 Oct 09 '09 at 06:22
  • 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 Oct 09 '09 at 06:27
  • @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 Oct 09 '09 at 06:32
2

change the line:

#define _ F-->00 || F-OO--;
Josh Lee
  • 171,072
  • 38
  • 269
  • 275