Kev1n91 Kev1n91 - 2 days ago 4
C Question

Define data type of enum in C for memory

I could not find a direct answer to is but is it possibel to force a certain kind of data type for an enum in C ?

e.g I have an enum for a state machine that will only hold some of the states, so for memory and perfomance issues it would be great to define an enum as a byte or short datatype. Is there any way to provide this behaviour in C or even in the Arduino IDE ?

Any help is appreciated

Answer

Enums are not "stored" as variables use memory locations. Enums are used in statements. These statements are compiled and in the compiled result (machine code, assembler), the enums are replaced with their value. For example, an enum constant named MY_ENUM1 with value 1 will be replaced in the assembler as

    mov ax, 1

Hence they will take up the smallest amount of memory as is required for the instruction operand.

Comments