Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!columbia!rutgers!sunybcs!boulder!hao!husc6!cmcl2!brl-adm!adm!RMRichardson.PA@Xerox.COM From: RMRichardson.PA@Xerox.COM (Rich) Newsgroups: comp.lang.c Subject: Re: Enum vs Define Message-ID: <8572@brl-adm.ARPA> Date: Thu, 30-Jul-87 16:43:27 EDT Article-I.D.: brl-adm.8572 Posted: Thu Jul 30 16:43:27 1987 Date-Received: Sat, 1-Aug-87 10:35:27 EDT Sender: news@brl-adm.ARPA Lines: 76 In article <729@jenny.cl.cam.ac.uk> Alan Mycroft writes: >In article <196@dbase.UUCP> awd@dbase.UUCP (Alastair Dallas) writes: >> (what is the difference between) >> enum { >> ERRMSGA = 1, >> ERRMSGB = 2 >> }; >>and >> >> #define ERRMSGA 1 >> #define ERRMSGB 2 >> >Here's one which is facetious, but none-the-less risky if you haven't >thought carefully: >If I say > if (ERRMSGA == ERRMSGB) > ... >then ... is always executed in both cases. >Now, if I do > #if (ERRMSGA == ERRMSGB) > ... > #endif >then the ANSI-C draft REQUIRES(!!!!!) >the first case NOT to compile ... and the second case to compile ... . >Put simply, all undefined macros (including enum constants) are treated >as 0 by the pre-processor, often (sadly) without warning. Pardon me, but I think of the four cases the first two compile and do NOT execute (that is, (1 == 2) is false), the third compiles because the two macro names are undefined (thus replaced by 0 and 0 == 0, which is true) and the fourth fails to compile because (1 == 2) is false. From H&S (2nd ed.) pg. 41 sec. 3.5.1 The #if, #else, and #endif Commands > ... > If an undefined macro name appears in the constant-expression > of #if or #elif it is replaced by the integer constant 0. This > means that the commands "#ifdef name" and "#if name" will have > the some effect as long as the macro name, when defined, has a > constant, arithmetic, nonzero value. We think it is much > clearer to use #ifdef or the defined operator in these cases, > but even Draft Proposed ANSI C supports the use of #if. (Has the Draft changed?) I assume the statement: enum { ERRMSGA = 1, ERRMSGB = 2 }; would not cause the preprocessor to take ERRMSGA and ERRMSGB as anything other than undefined macro names; thus, #if (ERRMSGA == ERRMSGB) becomes #if (0 == 0). If the tests were: if (ERRMSGA != ERRMSGB) #if (ERRMSGA != ERRMSGB) then I would expect the first two cases to compile and execute, the third case to be excluded from the compile, and the fourth case to compile and execute. I've been looking at this message for a week or so trying to find something arcane that would make the claims correct, unsucessfully. Either the tests are backwards or I may never understand C. (:-) When I read the section on #if in K&R, I thought the preprocessor might give an error because of the undefined names, but H&S (and the Draft?) is quite specific -- there is no warning. Did I miss anything here? Rich