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;
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • Does this answer your question? [Why use enum when #define is just as efficient?](https://stackoverflow.com/questions/5243269/why-use-enum-when-define-is-just-as-efficient) – Raymond Chen Mar 10 '20 at 03: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. – Jonathan Leffler Mar 10 '20 at 04:06

1 Answers1

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
{
};
robthebloke
  • 9,331
  • 9
  • 12
  • 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…” – Eric Postpischil Mar 10 '20 at 07:30