quick, easy: what is enum?

Joined
Apr 4, 2003
Messages
836
that is, how is it represented by the machine? an integer?

i've currently got no real reason to know, as my program works fine... but i don't like using contructs of a language without knowing how they work from at least an abstract level.
 
I'll presume you're asking about C++. The standard says that a compiler should choose the smallest integer type that holds the range of an enumeration. So

Code:
enum fooey { n = 3, m = 4 };

should make fooeys a byte, while

Code:
enum booger { x = 34, y = 19341345 };

should make boogers a four-byte type.

In practice, some compilers don't do this and always use their natural "int" type.

If you want to know, you should evaluate sizeof() on your enum type.
 
mikeblas said:
I'll presume you're asking about C++. The standard says that a compiler should choose the smallest integer type that holds the range of an enumeration. So

Code:
enum fooey { n = 3, m = 4 };

should make fooeys a byte, while

Code:
enum booger { x = 34, y = 19341345 };

should make boogers a four-byte type.

In practice, some compilers don't do this and always use their natural "int" type.

If you want to know, you should evaluate sizeof() on your enum type.

thanks, i WILL check the sizeof() for different values of my enum.
 
Different values in the same enum will have the same size.
(Different enums might have different sizes, though.)
 
nameless_centurian said:
interesting.

Not really. When you say that a variable is of type enum (something), it's represented as a type wide enough for the largest value of that enum, which isn't something that changes while running. So the width used to represent any given enum is constant.
However, that width doesn't have to be the same for different enums.
 
HHunt said:
Not really. When you say that a variable is of type enum (something), it's represented as a type wide enough for the largest value of that enum, which isn't something that changes while running. So the width used to represent any given enum is constant.
However, that width doesn't have to be the same for different enums.

oh, i misunderstood.

i thought you meant that if i have say, 256 elements in an enum that the first 255 would be byte, and the last would be 16 bits. my apologies.
 
Back
Top