Hey, what does the following code do?

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

I found this interesting question from http://www.techfest.org/archives/tf07/wired_logic/codemaestros/code.c

That program computes an approximation of PI by computing its own area. If you want to get more digits out of it, write a bigger program. [ 1 ]

BTW. That one's been around for years. It's one of the oldest entries in the International Obfuscated C Code Contest .

>computing its own area.

Whose area? I didn't get it.

>write a bigger program.

How? By increasing the number of _-_-......-_-_ ?

I would really appreciate if someone knows and could explain how the program works.

It's just a giant math expression.

Why are you trying to learn programming by looking at programs that are specifically designed to be hard to understand?

You misunderstood me. I've already learned basic-level C programming. When I came across this program, I was very much curious to know how it works. I love the intricacies of the language. Once again,

I would really appreciate if someone knows and could explain how the program works.

Ah.

It's just a giant math expression.

If you want to see it in action why don't you write something that traces the values of the variables F and OO?

If you do, you'll see that it uses a cheap trick based upon the circular shape of the _-_ sequences.

Each line of the circle separates an expression.
Each _ becomes (essentially) F--; OO--; and each -_ that follows it on the line becomes F--; That's the first cheap trick. The second cheap trick is where the lines separate. You'll notice that the lines separate around the circumference of the circle. The physical shape of the program affects the calculation --that is, F is modified each statement, but OO is only modified once per line. The amazing crazyness is that the circular shape is itself an approximation of PI. If you were to change it to a square or any other shape it wouldn't work. You can add more _-_'s to get a more accurate approximation, but the shape must remain a circle. (In other words, as you increase the resolution of the graph, you increase the accuracy of the computation.)

More than this you'll have to think about it yourself. Good luck.

Thank you :). That really helped me a lot.

Still, I'm not able to understand this part of the printf statement.

printf("%1.3f\n",[B][U]4.*-F/OO/OO[/U][/B]);

The global variables F & OO are modified by the function F_OO(). I agree that

F is modified each statement, but OO is only modified once per line.

Surprisingly, when I put a printf statement in between to get values of F & OO, both F & OO turn out to be -16 and the final output is 0.250.
How can F & OO be eqaul if

F is modified each statement, but OO is only modified once per line.

is true?

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

I get the following output: -201 -16 3.141 Did you compile with gcc -traditional-cpp ?


The printf statement just uses as few characters as needed to work. 4. floating point number * -F since F is negative, make it positive. (4.0 * (-F)) is a float. /OO float division (since numerator is float) /OO float division


Enjoy.

commented: You've helped me a lot :) +2

Ah! It is the compiler again. I used TC++ 3.0. I think that it is time for me to switch over to gcc. Thank you :)

Actually, it is the preprocessor: cpp. Modern preprocessors are a lot smarter than they used to be. They actually tokenize the input. By using the "traditional" cpp mode, where the strings are simply replaced by new text in the input stream, the code is transformed properly for this program. Remember: -_ becomes: --F<00||--F-OO--; Without that minus sign out in front of the underscore, the first condition is just: -F<00 , which is incorrect.

I use MinGW happily. If you go to their site on sourceforge, you'll get a huge list of weird file names, like: gcc-ada-3.4.2-20040916-1.tar.gz. The first part (gcc-ada) is the package name. The second part is the version number (3.4.2). The third part is the date the package was compiled. The last part (1) is the package type. And finally, of course, the file extension.

You'll need/want the following packages:
gcc-core
gcc-g++
mingw-runtime
mingw-utils
binutils
mingw32-make
w32api

Look for stuff in the "current" section first. Otherwise try to get the latest version. You want the package type to be "bin". It will either say "bin" or nothing. Don't get "src" or anything else... Download them all to C:\MinGW\. Unpack them. Some come as exe. Some as zip. Some as tar.gz. Once unpacked you can delete the files you downloaded. Add C:\MinGW\bin to your path, and you're good to go.

(Personally, I prefer to keep it in C:\PROGRA~1\MinGW, it works fine. However, some utilities can't handle spaces in pathnames. For example, if you were to download and compile Boost, you'd have to move the MinGW directory to the root temporarily to use bjam, then move it back when done.)

Hope this helps.

Yup, this really helped a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.