extern(C) enum

Timothy Foster via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 16 03:06:24 UTC 2017


On Friday, 15 September 2017 at 19:35:50 UTC, nkm1 wrote:
> On Friday, 15 September 2017 at 19:21:02 UTC, Timothy Foster 
> wrote:
>> I believe C enum size is implementation defined. A C compiler 
>> can pick the underlying type (1, 2, or 4 bytes, signed or 
>> unsigned) that fits the values in the enum.
>
> No, at least, not C99. See 6.4.4.3: "An identifier declared as 
> an enumeration constant has type int". You must be thinking 
> about C++.

You are correct, however 6.7.2.2 "Enumeration specifiers" states: 
"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."

I believe that means that if you have the following:

enum ABC { A, B, C }

Then A, B, and C are by themselves ints, but the enum type ABC 
can be a char if the compiler decides that's what it wants it to 
be.


More information about the Digitalmars-d-learn mailing list