[comp.std.c] Is it only ANSI C that allow #if to compare symbols?

andrew@resam.dk (Leif Andrew Rump) (01/19/91)

Hello netters - please help me with this one - this will be the last one -
and if you believe that then I have an Eiffeltower to sell you!!!  :-)

I tried this construction on our Sun C compiler (please no flames!):

#define	FOLDER	fk
...
#ifdef	FOLDER
  part 1
#endif
...
#if	FOLDER == fk
  part 2
#endif

It worked OK for part 1 i.e. if FOLDER is defined (never mind it's
value) then part 1 if compiled but part 2 get compiled under any
circumstances _even_ if you only define one of two symbols and write:

#ifdef	FOLDER == CASE
  part 2
#endif

So this rules out that the presence is equal 1 and absence equal 0!
The manual states "#if constant-expression ... All binary non-assign=
ment C operators, including '&&', '||' and ',', are legal ... The '?:'
operator, and the unary '-', '!', and '~' operators, are also  ..."

Is there any other way I may check a symbols value?

Any suggestions?

Leif Andrew

Leif Andrew Rump, AmbraSoft A/S, Stroedamvej 50, DK-2100 Copenhagen OE, Denmark
UUCP: andrew@ambra.dk, phone: +45 39 27 11 77                /
Currently at Scandinavian Airline Systems                =======/
UUCP: andrew@resam.dk, phone: +45 32 32 51 54                \
SAS, RESAM Project Office, CPHML-V, P.O.BOX 150, DK-2770 Kastrup, Denmark

> > Read oe as: o <backspace> / (slash) and OE as O <backspace> / (slash) < <

diamond@jit345.swstokyo.dec.com (Norman Diamond) (01/19/91)

In article <1991Jan18.170921.4866@resam.dk> andrew@resam.dk (Leif Andrew Rump) writes:

>#define	FOLDER	fk
>#ifdef	FOLDER
>  part 1
>#endif
>#if	FOLDER == fk
>  part 2
>#endif
>
>It worked OK for part 1 ... but part 2 get compiled under any circumstances
>
>#ifdef	FOLDER == CASE
>  part 2
>#endif
>
>So this rules out that the presence is equal 1 and absence equal 0!

Absence equals zero, yes.  Presence equals whatever the defined value is.
Now let's see what happens when you test
#if    FOLDER == fk
#if    fk     == fk
#if    0      == fk
#if    0      == 0
which is true.

#ifdef FOLDER == CASE
is syntactically invalid.  Assume you meant #if and let's see:
#if    FOLDER == CASE
#if    0      == CASE
#if    0      == 0

If you want a macro to evaluate to something other than 0, you must give
it a value (a replacement-list) that evaluates as a constant-expression.
Doug Gwyn already posted correct examples.

--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.