Conditional #if preprocessor expressions

Stephen Clamage steve at taumet.com
Fri Apr 19 01:34:28 AEST 1991


noren at dinl.uucp (Charles Noren) writes:

>I have defined a macro symbol, INCL to be:
>  #define INCL apple
>I then use INCL in the following expressions:

>  #if INCL == apple
>  #include "apple.h"
>  #endif

>  #if INCL == orange
>  #include "orange.h"
>  #endif

>My expectation is that only the include file "apple.h"
>is included.  However, the include file "orange.h" is
>included as well.  Why is this? 

The preprocessor does not do string comparison, just integer arithmetic.
The preprocessor rule is that if a name has not been #defined, its
value is zero.  So INCL is defined to be apple, which apparently has
not been #defined.  The value of apple is then 0, and so the value of
INCL is 0, and the two are equal; "apple.h" is then #included.

Next, INCL is compared to orange, which has not been #defined, so its
value is 0, the same as the value of INCL; "orange.h" is then #included.

What you want is something more along the lines of

	#define APPLE	/* or ORANGE, or whatever */

	#ifdef APPLE
	#include "apple.h"
	#endif

	#ifdef ORANGE
	#include "orange.h"
	#endif
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list