Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 23. May 2024, 22:28:26
Autres entêtes
Organisation : None to speak of
Message-ID : <87msog43r9.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Michael S <
already5chosen@yahoo.com> writes:
On Thu, 23 May 2024 17:10:38 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
[...]
Are C23 enums signed? or unsigned? What is the supported enum
range?
>
I never read the standard, so below is *according to my understanding*,
rather than the fact.
Before C23 - signed, at least as wide as int, but wider ranges are not
prohibited and can be provided by implementation.
C23 - enum without type specifier are the same as before. enum with type
specifier have range of their master type.
In C17 and earlier:
Each enumerated type shall be compatible with char, a signed integer
type, or an unsigned integer type. The choice of type is
implementation-defined, but shall be capable of representing the
values of all the members of the enumeration.
The compatible type can be either signed or unsigned, as documented by
the compiler. Each enumerator is of type int (not, as one might expect,
of the enumeration type).
enum foo { foo0, foo1 };
enum foo obj;
obj is of type "enum foo", which may be compatible with any integer type
but must be able to represent all the values of the enumerators.
enum bar { x = -1, y = +1 };
The compatible type for enum bar must be a signed type.
C23 adds the term "underlying type" for the type with which an enum type
is compatible. It can be specified explicitly; if not, it's an
implementation-defined type as in C17 and earlier.
For an enum type without a specified underlying type, enumerators are
usually of type int, but can be of the enum type in some cases. For
example:
enum big { a = INT_MAX, b };
The underlying type is likely to be long or long long, and the
enumerators a and b are of type enum big, which is wider than int.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */