Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion 42.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
#include <stdio.h>

#if defined ORIGINAL
/* retained for literary criticism */
#define SIX 1+5
#define NINE 8+1
#elif ! defined INSANE
/* a sensible reimplementation */
#define SIX 3+3
#define NINE 15-6
#else
/* a less sensible reimplementation */
#define SIX 24-18
#define NINE 1/-2+9
#endif

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
printf("%d × %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ around each reference to a macro parameter, and around the entire definition.
The reason is that macro expansion works on sequences of tokens.
The C preprocessor doesn't deal with high-level C syntax.

In the example, the macro `SIX` expands to the token sequence `1 + 5`,
and the macro `NINE` expands to `8 + 1`. If each of these expressions
In the example, unless `ORIGINAL` is defined, the macro `SIX` expands to the token sequence `3 + 3`,
and the macro `NINE` expands to `15 - 6`. If each of these expressions
were evaluated on its own, the results would be 6 and 9, respectively,
But when they're combined in the expression `SIX * NINE`, the expansion is:

1+5*8+1
3+3*15-6

which is equivalent to:

1+(5*8)+1
3+(3*15)-6

or 42.

The program is also a reference to [a joke](http://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29) in The Hitchhiker's Guide to the Galaxy, by Douglas Adams.

-- Keith Thompson <Keith.S.Thompson@gmail.com> Tue 2011-11-01

I posted this program to comp.lang.c on Mon 2003-09-29
Based on a program I posted to comp.lang.c on Mon 2003-09-29
with this comment:

> (Not original; I don't remember where I got it.)