Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!tdatirv!sarima From: sarima@tdatirv.UUCP (Stanley Friesen) Newsgroups: comp.lang.c Subject: Re: Are enums safe to use in portable code? Message-ID: <128@tdatirv.UUCP> Date: 4 Feb 91 21:48:22 GMT References: <22898@well.sf.ca.us> <1991Jan30.210255.16804@csrd.uiuc.edu> Reply-To: sarima@tdatirv.UUCP (Stanley Friesen) Organization: Teradata Corp., Irvine Lines: 36 In article enag@ifi.uio.no (Erik Naggum) writes: >Assume these definitions: > enum { frotz, klutz } foo; [1] > enum { gunk, junk } bar; [2] > ... >The declaration in [1] declares a distinct (anonymous) type of which >foo is an object. Likewise [2] declares another distinct (anonymous) >type of which bare is an object. So far so good. ANSI does indeed specify that each enumeration is a distinct type. > frotz and klutz [1] are names of >constant values of the type of object of which foo is an instance. >Likewise for gunk and junk [2]. However here you deviate from the ANSI standard. According to 3.5.2.2 (Semantics): "The identifiers in an enumerator list are declared as constants that have type int and may appear anywhere such are permitted". Thus frotz, klutz, gunk, and junk are all *int's*, and may be used anywhere an int may be used. This includes *all* forms of comparison, thus: (klutz == gunk) is a perfectly valid ANSI C construct, and evaluates to true if and only if the value of klutz equals the value of gunk. (This is also a constant expression, that can be evaluated wholly by the compiler) Any C compiler that fails to accept this is *not* ANSI C. (It may be a valid pre-ANSI compiler though). -- --------------- uunet!tdatirv!sarima (Stanley Friesen)