#if foo vs #if defined(foo)

Gregory Smith greg at utcsri.UUCP
Sat Jun 28 01:56:15 AEST 1986


In article <379 at twitch.UUCP> grt at twitch.UUCP ( G.R.Tomasevich) writes:
>Our compiler does not need a defined(), or at least I did not try whether
>it is even accepted.  The following example works fine:
>
>#if squat||foo
>int yes;
>#else
>int no;
>#endif
>main() {}
>
>if the 'cc' command includes either '-Dsquat' or '-Dfoo', then 'yes' is
>selected, else 'no' is selected.  E.g.: 'cc -Dfoo -E c.c'
>System: VAX-11/785 running UNIX 5.2
>-- 

This seems very wrong to me. If foo is defined (as 1), and squat isn't,
shouldn't that come out as

#if squat||1

which should be rejected by the cpp as containing a non-constant ?

What if '#define squat 0' and '#define foo 0' ?? The expression
becomes 0||0 which is false.

This compiler here (4.2BSD) backs me up on the second point, but not
on the first ( i.e. the given example works ). Apparently any symbol in
a #if expression becomes '0'.

What if squat or foo are defined as something other than a constant?
This device cannot replace #if defined(a)|| defined(b) in general.

-- 
"Shades of scorpions! Daedalus has vanished ..... Great Zeus, my ring!"
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list