Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: clarification about enums in C Date: Thu, 27 Feb 2020 06:33:07 -0800 Organization: None to speak of Lines: 34 Message-ID: <87imjst80s.fsf@nosuchdomain.example.com> References: <5c585469-72c8-42f8-9e74-848f160db602@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="8e8b5cc6cc98845fdc41837a5320285f"; logging-data="26179"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Mqe4TEfpiiUAH4q5+oz7g" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:prLxcqT27SzC7pDWcNJkjbbGcGE= sha1:B22OBlAckI9VWxt0a13j3GjC2Wk= Xref: csiph.com comp.lang.c:150297 David Brown writes: [...] >> enum enum_t { >> uns = (unsigned)-1, >> neg = -1, >> }; >> Both in gcc and in clang uns has type 'unsigned int' rather than 'int'. >> And indeed it differs from type of 'enum_t' which in ths case is 'int'. > > This is an extension that these tools support - as do many compilers. > If you want to ensure that you stick to standards-only code, use > "-Wpedantic" (along with your choice of -std=c11, -std=c99 or -std=c90), > and pay attention to the warnings. The C standard does not say how > "uns" should be treated, gcc's extensions do the "obvious" thing. Ah, but gcc doesn't print a diagnostic with "-std=c11 -Wpedantic", even though the code violates a constraint. N1570 6.7.2.2p2: The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int. Even my copy of gcc built from source that calls itself gcc (GCC) 10.0.0 20200111 (experimental) fails to diagnose it. A recent version of clang does print a diagnostic. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */