Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!mit-eddie!genrad!decvax!decwrl!pyramid!oliveb!sun!gorodish!guy From: guy%gorodish@Sun.COM (Guy Harris) Newsgroups: comp.lang.c Subject: Re: Enum vs Define Message-ID: <21682@sun.uucp> Date: Sun, 21-Jun-87 17:41:25 EDT Article-I.D.: sun.21682 Posted: Sun Jun 21 17:41:25 1987 Date-Received: Tue, 23-Jun-87 00:50:05 EDT References: <196@dbase.UUCP> Sender: news@sun.uucp Lines: 43 Keywords: enum, define > Why use enum instead of #define? For example, if I've > got an array of error message strings indexed with ERRMSGA, ERRMSGB > identifiers, isn't it more robust to explicitly define the values > (which must correspond to the array) than to let the compiler do it? Absent a mechanism for declaring arrays with "enum"s as subscripts, the #defines may be better *in this case*. However, this is NOT a typical example of the use of "enum"s. Most uses of "enum" do not consider the numerical value used to represent the various values of the "enum" to be important. In those cases, the advantages would be: 1) Better type checking. The compiler will complain (or, one would hope, warn, even in an ANSI C compiler) about mixing one "enum" type with another. Such mixing rarely makes sense (the compiler should probably allow the programmer to use casts to indicate that they have determined that mixing does make sense in some particular case). 2) Better interfaces with debuggers. If you declare enum state { START, STATEA, STATEB, FINAL }; a good debugger, when asked to print the value of "state", will print it as "START", "STATEA", "STATEB", or "FINAL". However, if you declare int state; #define START 0 #define STATEA 1 #define STATEB 2 #define FINAL 3 and ask it to print "state", it will print the numerical value and force you to translate it to something meaningful. If you could declare arrays indexed by "enum"s, I would use them as error message indices as well. Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com