Conditional #if preprocessor expressions

Charles Noren noren at dinl.uucp
Thu Apr 18 02:47:45 AEST 1991


This is my day to ask stupid questions....

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? 
(I know there must be an obvious answer...)
BTW, some examples in nihcl use this construct.  See
ex11-1.c under the ex directory.

My environment:

 o  Sun Spac station 2.
 o  SunOS 4.1.1
 o  Saber C++ 1.0
 o  also compiled under the default Sun cc.

Sample program:

======main.c==================================
/*
 * File: main.c
 *
 */

#define INCL apple

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

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

#include <stdio.h>

char* myString = FRUIT;

main()
{
    printf("myString = [%s].\n", myString);
}
==============================================

======apple.h=================================
/* File: apple.h  */

#define FRUIT "apple"

==============================================

======orange.h================================
/* File: orange.h  */

#define FRUIT "orange"

==============================================


Thanks,


-- 
Chuck Noren
NET:     dinl!noren at ncar.ucar.edu
US-MAIL: Martin Marietta I&CS, MS XL8058, P.O. Box 1260,
         Denver, CO 80201-1260
Phone:   (303) 977-1646



More information about the Comp.lang.c mailing list