enumeration variables

A

aarklon

Hi all,
in the article
http://www.c-faq.com/struct/enumvsdefine.html

it is written as

Some advantages of enumerations are that the numeric values are
automatically assigned, that a debugger may be able to display the
symbolic values when enumeration variables are examined, and that they
obey block scope. (A compiler may also generate nonfatal warnings when
enumerations are indiscriminately mixed, since doing so can still be
considered bad style even though it is not strictly illegal.) A
disadvantage is that the programmer has little control over those
nonfatal warnings; some programmers also resent not having control over
the sizes of enumeration variables.

my question is

why is it written as sizes of enumeration variables are not under the
control of programmer
after all as per my understanding goes enumerations are integral data
types,so their
size should be less than or equal to that of int isn' t it???
 
M

Mike Wahler

Hi all,
in the article
http://www.c-faq.com/struct/enumvsdefine.html

it is written as

Some advantages of enumerations are that the numeric values are
automatically assigned, that a debugger may be able to display the
symbolic values when enumeration variables are examined, and that they
obey block scope. (A compiler may also generate nonfatal warnings when
enumerations are indiscriminately mixed, since doing so can still be
considered bad style even though it is not strictly illegal.) A
disadvantage is that the programmer has little control over those
nonfatal warnings; some programmers also resent not having control over
the sizes of enumeration variables.

my question is

why is it written as sizes of enumeration variables are not under the
control of programmer
after all as per my understanding goes enumerations are integral data
types,so their
size should be less than or equal to that of int isn' t it???

'int' is not the only (or the largest) integral type.
The size of the largest integral type (long in C89,
long long in C99) is implementation dependent. An
implementation will typically choose the appropriately
sized integer type for a given enum.

-Mike
 
B

Ben Pfaff

why is it written as sizes of enumeration variables are not
under the control of programmer after all as per my
understanding goes enumerations are integral data types,so
their size should be less than or equal to that of int isn' t
it???

The compiler gets to choose the particular type that a
enumeration type is compatible with. It's an integer type but
the programmer doesn't get to say which one.
 
D

Dave Thompson

On Sat, 29 Apr 2006 17:53:15 GMT, "Mike Wahler"
'int' is not the only (or the largest) integral type.
The size of the largest integral type (long in C89,
long long in C99) is implementation dependent. An
implementation will typically choose the appropriately
sized integer type for a given enum.
True but not very relevant. Enumeration _constants_ (the identifiers
within the braces enum { xxx [ = n ] } ) must be within the range of
'int', so there is no _good_ reason for a compiler to select a type
wider than int for an enum type. OTOH where it makes a difference for
speed or size it may be a good idea to select a narrower type for an
enum type whose required range (smallest to largest values specified
or defaulted within the braces) is small enough.

<OT> In C++(98) enum constants can 'grow' to long, and thus so can
enum types using them. I don't know what will be done if and when C++
adopt's C99's long long. </>

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top