5

To ensure binary compatibility with the product I'm working on, I need to force some of my enums to be represented by a single byte.

Since C90 allows for the compiler to chose an appropriate type (6.7.2.2 Enumeration specifiers), I would like to force the compiler to chose 8 bits.

I did expect GCC attributes to allow this, but I could not find anything here. Is there another way to handle this? (or an attribute I missed)

I'm using GCC 4.6.4 for Microblaze and C90 (I did not see a relevant option here either).

Simon Doppler
  • 1,918
  • 8
  • 26
  • C++11 allows you to declare the enum type, see [here](https://en.cppreference.com/w/cpp/language/enum). I just noticed it's [tag:c] (I am listening to c++ and c). But will leave comment as others might find it useful if they reach the quetion. – Tony Tannous Nov 27 '19 at 11:19
  • 1
    Try [-fshort-enums](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options) as suggested in on of the [answers](https://stackoverflow.com/questions/4879286/specifying-size-of-enum-type-in-c) – Tony Tannous Nov 27 '19 at 11:43
  • Or used [packed](https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html) attribute. "This attribute, attached to an enum, struct, or union type definition, specified that the minimum required memory be used to represent the type." – Tony Tannous Nov 27 '19 at 11:50
  • @TonyTannous Thanks, I will try your suggestions (I appear to have missed the `packed` attribute). – Simon Doppler Nov 27 '19 at 12:14
  • 1
    Enumerated values don't need to be stored in an enumerated type. You can store the value in a signed or unsigned, single-byte integer type according to your needs. (See [this answer](https://stackoverflow.com/a/54921765/5264491).) – Ian Abbott Nov 27 '19 at 13:05

0 Answers0