How Enums Are Handled In C

Ozan Ağma
2 min readApr 28, 2021

--

Hello. I wrote a simple code to understand how enum items is being handled. I have 2 enum types and one define constant. The reason I defined two enums(name, surname) is to understand if its size depends on the enum item values.

Code:

After preprocessor:

To obtain preprocessed file in Visual Studio you should follow these steps: Settings -> C/C++ -> Preprocessor -> Preprocess to a File: Yes(/P). Go to debug folder and open a file with .i extension.

As you see only thing changed is #define constant. Preprocessor has no jobs with enum items.

After Compiling:

To obtain compiled file in Visual Studio you should follow these steps: Settings -> C/C++ -> Output Files -> Assembler Output: Assembly With Source Code(/FAs). Go to debug folder and open a file with .asm extension.

Compiler converted all enum items to constants. This explains why we dont have pointer of enum items because they are constant like 5 or 78. Unlike enum items “a” variable is computed with its value in the memory. Finally we can see that enum item size does not depend on its value and always 4 bytes.

--

--

Ozan Ağma
Ozan Ağma

No responses yet