[comp.lang.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.

gwyn@smoke.brl.mil (Doug Gwyn) (01/22/91)

In article <1991Jan21.090551.12909@resam.dk> andrew@resam.dk (Leif Andrew Rump) writes:
-The (Danish version of the) Ansi K&R compiler gives the following example:
[I assume you mean that some manual for some compiler gives the example.]
-#define	SYSTEM	SPARC
-#if	SYSTEM == SPARC
-  ...
-#else
-  ...
-#endif
-I was not able to find the book this morning but i _don't_ remember any
-defines giving the symbol SPARC any numerical value.

If not, then the example is incorrect.
(Well, actually it would work due to the expression being evaluated as 0==0,
but SYSTEM cannot be used to select among different system types in this way
unless each system-type macro is given a distinct value.)

rtm@christmas.UUCP (Richard Minner) (01/22/91)

In article <14900@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>	...
>	#if TEST == VAL_A
>	...
>	#elif TEST == VAL_B
>	...
>	#fi

#fi ?  Sorry Doug, X3J11 considered it but decided it wasn't necessary.
(Hey, someone had to point it out or we'd end up with posts saying
"my compiler is supposed to be ANSI compatible but it doesn't support
`#fi'.  Isn't that part of ANSI-C?")

-- 
Richard Minner  rtm@island.COM  {uunet,sun,well}!island!rtm
Island Graphics Corporation  Sacramento, CA  (916) 736-1323

gwyn@smoke.brl.mil (Doug Gwyn) (01/27/91)

In article <31@christmas.UUCP> rtm@island.COM (Richard Minner) writes:
>#fi ?

Yeah, sorry, I had just finished writing a bunch of shell scripts.