Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!mcsun!ukc!edcastle!gtoal From: gtoal@castle.ed.ac.uk (G Toal) Newsgroups: comp.sys.acorn Subject: Re: C release 3 preprocessor bug ?? Message-ID: <10383@castle.ed.ac.uk> Date: 18 May 91 20:45:10 GMT References: <1991May16.125325.15323@doc.ic.ac.uk> Organization: Edinburgh University Lines: 36 In article <1991May16.125325.15323@doc.ic.ac.uk> mybg@doc.ic.ac.uk (M Y Ben Gershon) writes: > >Am I crazy, or is there a subtle bug in C release 3? I am surprised that >nobody has mentioned it so far, but the ANSI standard, as far as I can >gather, states that if a preprocessor macro is undefined, it is deemed to >have a value of 0 (in fact 0L, to be precise). According to the latest K&R, >(which is the ANSI version), and Harbison & Steele, a macro defined as being >0 is identical to a macro which is undefined, as above. However, Cv3 treats >them differently: If a macro is defined as: > > #define VAX 0 > >and we then do: > > #ifdef VAX > #error VAX is defined! > #endif > >the error message will be displayed. By the way, who noticed the section on Which reminds me of a general point when porting programs - for progs with lots of target machines, some authors tend to use #if rather than #ifdef so that they cal say things like '#if VAX || (UNIX && BSD42)' -- so they have a header file where *all* the possible symbols are defined - most as 0, one as 1. Now a modern ANSI C programmer comes along to port it, and accidentally puts in a '#ifdef VAX' (or more likely '#ifndef VAX') and is suprised when the wrong thing happens. It is *very* hard not to accidentally use ifdef in these situations - the supplied '#if' style being a bit non-obvious. Nowadays I use '#if defined(VAX) || defined(BSD42)' almost exclusively. Much safer. But of course, old compilers will treat expressions with 'defined()' in them as an error and assume the expression is FALSE. ho hum. Graham