Conditional #if preprocessor expressions

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Thu Apr 18 19:20:34 AEST 1991


In article <1849 at dinl.mmc.UUCP>, noren at dinl.uucp (Charles Noren) writes:
>   #define INCL apple

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

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

It doesn't work.  The reason is that, as stated in C manuals and textbooks,
the operand of an #if directive is an *integer* constant expressions, and
identifiers which are not defined as macros are in effect replaced by 0.
Hence
	#if INCL == orange
turns into
	#if apple == orange
which behaves like
	#if 0 == 0

The answer is to make sure that apple and orange _are_ defined:
	#define apple 1
	#define orange 2
some time before the first use of apple or orange.

-- 
Bad things happen periodically, and they're going to happen to somebody.
Why not you?					-- John Allen Paulos.



More information about the Comp.lang.c mailing list