problem with /define in VAX C: help!

Daniel Elbaum elbaum at reed.UUCP
Fri Jun 21 10:28:25 AEST 1991


In <1991Jun20.030147.3867 at athena.mit.edu> mlevin at jade.tufts.edu writes:


:   I have the following problem. I want to do conditional compilation,
: based on the value of a variable given at compile time... if I have
the program:

: main()
: {
:   enum {AA, BB, CC} dd;
:   dd = DEF;
: #if DEF!=CC
:   printf("%d\n",dd);
: #endif
: }

[and DEF is defined as BB on the compiler's command line,
a Unix compiler includes the printf but Vax C doesn't.]

You won't be able to count on the availability of enumeration
constants at compile time.  Standard C even guarantees that
enumeration constants *don't* exist in the phase in which
conditional inclusion is evaluated (sec. 3.8.1, esp. footnote).

If the condition really needs to be evaluated at compile time
rather than run time, then using #defines rather than enums is
likely the most straightforward way out.  But the code can remain
almost the same if you postpone evaluation until run time:


 main()
 {
   enum {AA, BB, CC} dd;
   dd = DEF;
   if (DEF != CC)
     printf("%d\n",dd);
 }

Note that this code will fail to compile if DEF is not defined.
-- 
                      :                       Daniel Elbaum
 Responsible for all  :               tektronix!reed!elbaum
 disclaimed postings  :                              elbaum at reed.edu



More information about the Comp.lang.c mailing list