12 July 2008

Interesting...

42 - Wiki...

There is a joke amongst computer programmers that Deep Thought may have had some order of operations issues. The following code in the C programming language defines the macros SIX as "1 + 5" and NINE as "8 + 1", and then performs the computation "SIX * NINE". It returns the answer "42", because "SIX * NINE" is expanded by the computer to "1 + 5 * 8 + 1", and the multiplication takes precedence over the additions. This occurs because the macro expansion is textual, not logical — knowledgeable C programmers always surround the content of every macro and each instance of a macro argument with parentheses to avoid problems such as this one.

#include

#define SIX 1 + 5
#define NINE 8 + 1

int main(void)
{
printf( "What do you get if you multiply %d by %d? %d\n", SIX, NINE, SIX * NINE );
return 0;
}

Falsely assuming that the answer is indeed correct, that means that the answer to the ultimate question of life, the universe and everything would be 42.

No comments: