title: Forcing enum field to 8 bits in standard C with bitfields
body: <p>I saw few questions here about how to force an enum to 8 or 16 bits. The common answer was that it can be done in C++11 or higher. Unfortunately, I don't have that luxury.</p>
  
  <p>So, here is what I'm thinking. I only need the enum to be 8 bits when it's in a struct whose size I want to minimize. So:</p>
  
  <p><strong>Option A:</strong></p>
  
  <pre><code>typedef enum { A, B, C, MAX = 0xFF } my_enum;
  
  struct my_compact_struct
  {
      my_enum field1 : 8; // Forcing field to be 8 bits 
      uint8_t something;
      uint16_t something_else;
  };
  </code></pre>
  
  <p>I think most or all optimizers should be smart enough to handle the 8 bitfield efficiently.</p>
  
  <p><strong>Option B:</strong></p>
  
  <p>Not use an enum. Use typedef and constants instead.</p>
  
  <pre><code>typedef uint8_t my_type;
  
  static const my_type A = 0;
  static const my_type B = 1;
  static const my_type C = 2;
  
  struct my_compact_struct
  {
      my_type field1; 
      uint8_t something;
      uint16_t something_else;
  };
  </code></pre>
  
  <p>Option A is currently implemented and seems to be working, but since I want to do (now and in the future) what's correct now and not just what's working, I was wondering if option B is clearly better.</p>
  
  <p>Thanks,</p>