Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from K&R C to ANSI C?

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.

like image 379
Vadakkumpadath Avatar asked Oct 09 '09 05:10

Vadakkumpadath


People also ask

How do we convert K?

Kelvin to Fahrenheit conversion is defined as converting a temperature from Kelvin to Fahrenheit unit. There are three units of measuring temperature - Kelvin, Fahrenheit, and Celsius. Converting Kelvin to Fahrenheit (K to F) is done by using the formula °F = (K − 273.15) × 1.8 + 32 or °F = (K − 273.15) × 9/5 + 32.

How do you convert K to N?

1 kelvin is equal to 0.33 N.

How do you convert C to K fast?

According to the Celsius to Kelvin formula, by adding 273.15, the temperature in Celsius can be converted to the temperature in Kelvin.

What is the formula of K to F?

The easiest formula for converting Kelvin to Fahrenheit is F = 1.8*(K-273) + 32. Both Kelvin and Fahrenheit are temperature scales. However, Kelvin is an absolute scale with its zero at absolute zero.


1 Answers

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.

like image 82
Jonathan Leffler Avatar answered Oct 06 '22 01:10

Jonathan Leffler