Skip to content

Keith-S-Thompson/42

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

This small C program demonstrates the danger of improper use of the C preprocessor.

A macro that's intended to be used in an expression context generally needs to be aggressively parenthesized. You should have parentheses 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 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

which is equivalent to:

1+(5*8)+1

or 42.

The program is also a reference to a joke in The Hitchhiker's Guide to the Galaxy, by Douglas Adams.

-- Keith Thompson [email protected] Tue 2011-11-01

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

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

About

A demonstration of improper use of the C preprocessor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages