[comp.lang.c] problem with /define in VAX C: help!

mlevin@jade.tufts.edu (06/20/91)

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

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

  And I compile it with: "cc a.c -DDEF=BB", the program will print out
1 when it runs, because DEF is assigned the enum value BB, which is
not equal to the enum value CC, so the printf goes in. But, on a VMS
system with Vax C, when I do cc a.c /define="DEF=BB", the printf is
never put in. I think it is because the compiler thinks DEF and CC are
both undefined at the point of the #if, assigns them both to 0, and
they are thus always equal (so the printf never gets put in). Am I
missing something (as usual)? Or is this a genuine difference between
Unix and Vax C compilers? In any case, I'd appreciate any help on how
I can do this in Vax C: I need to pass in an enum value during
compile-time. Please direct all suggestions to mlevin@jade.tufts.edu.

Mike Levin

elbaum@reed.UUCP (Daniel Elbaum) (06/21/91)

In <1991Jun20.030147.3867@athena.mit.edu> mlevin@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@reed.edu