Understanding Enum Size and Compiler Options in C (gcc)
This article examines how the memory size of C enum types varies with declared value ranges and compiler options such as -fshort-enums, showing default and shortened sizes across different compilers and illustrating the impact of adding larger constant values.
In this article the author investigates how large an enum type occupies in memory when compiled with GCC, demonstrating that the size depends on the declared value range and compiler options.
Using GCC 7.5.0 on Ubuntu 18.04, the default output for a sample enum is 4 bytes, but with the -fshort-enums flag the compiler can reduce the size to 1 byte when the values fit within an 8‑bit range.
Further experiments adding larger constants such as CMD_MAX_16BIT = 0xFFFF and 0xFFFFFFFF show the enum size increasing to 2 and 4 bytes respectively.
The article also notes that most compilers (GCC, Clang) do not enable -fshort-enums by default, while some embedded compilers like IAR do, and that the --enum_is_int option can force enums to be treated as int.
References to the GCC manual and IAR C/C++ Development Guide are provided, emphasizing the need to be aware of compiler‑specific behavior to avoid unexpected memory usage in embedded systems.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.