Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!laidbak!daveb From: daveb@laidbak.UUCP Newsgroups: comp.lang.c Subject: enum Message-ID: <1177@laidbak.UUCP> Date: Fri, 2-Oct-87 13:31:48 EDT Article-I.D.: laidbak.1177 Posted: Fri Oct 2 13:31:48 1987 Date-Received: Sun, 4-Oct-87 01:54:41 EDT Reply-To: daveb@laidbak.UUCP (Dave Burton) Organization: is pretty bad/My method of Lines: 67 Trick or Trap? A program I'm writing uses several ENUMs for variables and function return types. I have need to enforce a certain ordering of data flow, so I was comparing ENUM to ENUM as in the program below: main() { enum { A, B, C } varA, varB; printf("A < B is %c\n", A < B ? 'T' : 'F'); printf("A > B is %c\n", A > B ? 'T' : 'F'); printf("C == C is %c\n", B == B ? 'T' : 'F'); varA = A; printf("varA = A (%d)\n", varA); varB = B; printf("varB = B (%d)\n", varB); printf("varA < varB is %c\n", varA < varB ? 'T' : 'F'); printf("varA > varB is %c\n", varA > varB ? 'T' : 'F'); printf("varA == varA is %c\n", varA == varA ? 'T' : 'F'); exit(0); } Both lint and cc complain: "enum.c", line 12: illegal comparison of enums "enum.c", line 13: illegal comparison of enums I can shut them up and make the program work by casting varA and varB to int for the < and > operations. (i.e. (int)varA < (int)varB ). According to Harbison & Steele, "C: A Reference Book 2/e", section 5.5: "In addition to assigning values of enumeration types, the programmer can test two values for equality. Enumeration types are implemented by associating integer values with the enumeration constants, so that the assignment and comparison of values of enumeration types can be implemented as integer assignment and comparison. ... 2. The first enumeration constant receives the value 0 if no explicit value is specified. 3. Subsequent enumeration constants without explicit associations receive an integer value one greater that the value associated with the previous enumeration constant. ... As a matter of style, we suggest that programmers treat enumeration types as different from integers and not mix them in integer expressions without using casts." Questions: Is my solution portable? (I think so, but I can't test it.) Why does the constant comparison work but not the original variable comparison? As an aside: If you need an authoritative reference on the C language, the book by Harbison & Steele is an *excellent* investment, and is 'available in finer bookstores everywhere'. -- --------------------"Well, it looked good when I wrote it"--------------------- Verbal: Dave Burton Net: ...!ihnp4!laidbak!daveb V-MAIL: (312) 505-9100 x325 USSnail: 1901 N. Naper Blvd. #include Naperville, IL 60540