8 bit enum, in C

Lyndon White picture Lyndon White · Nov 9, 2009 · Viewed 13.3k times · Source

I have to store instructions, commands that I will be receiving via serial. The commands will be 8 bits long.

I need to preserve transparency between command name, and its value. So as to avoid having to translate an 8-bit number received in serial into any type.

I'd like to use Enumerations to deal with them in my code. Only a enumeration corresponds to a on this platform a 16 bit integer.

The platform is AVR ATmega169V microcontroller, on the Butterfly demo board. It is a 8bit system with some limited support for 16bit operations. It is not a fast system and has about 1KB of RAM. It doesn't have any luxuries like file I/O, or an operating systems.

So any suggestions as to what type I should be using to store 8-bit commands?
There has got to be something better than a massive header of #defines.

Answer

Michael Foukarakis picture Michael Foukarakis · Nov 9, 2009

gcc's -fshort-enums might be useful:

Allocate to an "enum" type only as many bytes as it needs for the declared range of possible values. Specifically, the "enum" type will be equivalent to the smallest integer type which has enough room.

In fact, here's a page with a lot of relevant information. I hope you come across many GCC switches you never knew existed. ;)