0

The two enum(s) in this code - what exactly are they doing? Why are they not simply defined as long or short? When I debug this status is 4 bytes. What is error_code datatype??

// make this VB compatible... #pragma pack (4) #ifndef IntVB #define IntVB short #endif typedef struct tagCommStatus { enum Comm status; enum CommErr error_code; IntVB nChannel; IntVB x_comm; IntVB y_comm; IntVB t_comm; IntVB z_comm; }CommStatus; 
2
  • Does this answer your question? Why use enum when #define is just as efficient? Commented Mar 10, 2020 at 3:41
  • The member status can only take on the values defined in enum Comm, which is defined elsewhere. Similarly, the member error_code can only take on the values defined in enum CommErr, which is also defined elsewhere. Yes, they could be replaced by a suitable integer type, but using the enum identifies the possible legitimate values better than just int does. The size of an enum is up to the compiler — 4 bytes is a legitimate choice. Commented Mar 10, 2020 at 4:06

1 Answer 1

0

status is a variable of type 'enum Comm' (or just Comm, if compiled as C++). error_code is a member variable of type 'enum CommErr'. You haven't included the code with the enum definitions, so it's hard to say for sure, although with names such as status and error_code, it's possible to take a guess.

In C, enums default to whatever size an integer is (i.e. probably 32bit on most platforms). In newer versions of C++, you can define the underlying data type for the enum if you wish, e.g.

enum Foo16 : int16_t { }; enum Foo8 : uint8_t { }; 
Sign up to request clarification or add additional context in comments.

1 Comment

Enumerations in C are not always int. The enumeration constants are, but the type is not always. C 2018 6.7.2.2 4 says “Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined…”

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.