I am working with embedded device, with 32K of memory, writing in plain C using IAR EWARM v6.30.
To make code more readable I would like to define some enum types, for example, something like
{RIGHT_BUTTON, CENTER_BUTTON, LEFT_BUTTON}
instead of using 0, 1, 2 values, but I am afraid it will take additional memory that is already scarce.
So I have 2 questions: 1) Can I force enum to be of short or byte type intead of int? 2) What is an exact memory imprint of defining enum type?
In fully compliant ISO C the size and type of an enum constant is that of signed int
. Some embedded systems compilers deliberately do not comply with that as an optimisation or extension.
In ISO C++ "The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration.", so a compiler is free to use the smallest possible type, and most do, but are not obliged to do so.
In your case (IAR EWARM), the manual clearly states:
No option required, in fact you'd need to use --enum_is_int
to force compliant behaviour. Other compilers may behave differently or have different extensions, pragmas or options to control this. Such things will normally be defined in the documentation.