I'm trying to work with C and Assembly (intelx8086) language.
I'm also using one class that was implemented by a friend of mine. It has a
typedef enum data_10 {a=0,b=7,c=10,} data_10_type;
I want to work with this class bitwise (AKA construct it/destroy it on Assembly). My question is, how much memory does "enum" take?
As enum types are handled as integral types, they are stored in the stack and registers as their respective data types: a standard enum is usually implemented as an int32; this means that the compiler will handle your enum as a synonym of int32 (for the sake of simplicity, I left out several details).
An enum does not really take any memory at all; it's understood by the compiler and the right numbers get used during compilation. It's an int, whose size is dependent on your system.
Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.
A standard enum is usually implemented as an int32, the compiler will handle your enum as a synonym of int32 . Once a list of values is created for a enumeration those values are stored as literals against their display name(access name given at the time of declaration of enum).
Although it may vary from compiler to compiler, enum
typically takes the same size as an int
. To be sure, though, you can always use sizeof( data_10_type );
Why don't you print it?
/* C99 */
#include <stdio.h>
typedef enum { a = 0, b = 7, c = 10 } data_10_type;
printf("%zu\n", sizeof(data_10_type));
The identifiers in an enumerator list are declared as constants that have type int
(C11 §6.7.2.2 Enumeration specifiers), so sizeof(data_10_type)
is often equal to sizeof(int)
, but it isn't necessary!
BTW, if you want to have a size in bits, just use the CHAR_BIT
constant (defined in <limits.h>
), which indicates how many bits there are in a single byte).
/* C99 */
#include <limits.h>
#include <stdio.h>
typedef enum { a = 0, b = 7, c = 10 } data_10_type;
printf("%zu\n", sizeof(data_10_type) * CHAR_BIT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With