question about an array of enum

Tim McDaniel mcdaniel at adi.com
Wed Nov 7 03:45:04 AEST 1990


gary at hpavla.AVO.HP.COM (Gary Jackoway) writes:
> I don't think a compiler can make enums smaller than int and be ANSI
> compliant, since the standard says that enums are ints.

You're mixing 'enumeration constants' and 'enumerated types'.  See
Section 3.5.2.2 of the ANSI Standard.  Enumeration constants (the
identifiers between the { and the }) are of type 'int'.  Then again,
so are character constants, like '*'.  "Each enumerated type shall be
compatable with an integer type; the choice of type is implementation-
defined."  So the enum value itself might be a 'char', say, if the
range of enumeration constants defined permits it.

> It is for this reason that I almost never use enums.  Their size
> varies from machine to machine and there is nothing you can do about
> it.

It is for this reason that I almost never use character constants like
'a'.  They're ints, so their size varies from machine to machine and
there is nothing you can do about it.

What C type DOESN'T vary in size from machine to machine?  (Answer:
none.  The Standard only sets minima, and in practice there is
variation.  I suspect that there are or will be compilers with 16 bit
or 32 bit 'char'.)

Why in creation you CARE how much space they take?  I can see only two
reasons: (1) binary files, (2) memory space.  Binary files are
inherently unportable.  Often, worrying about memory space is
worshipping the Little Tin God of Efficiency.  If you have a large
array AND have severely limited memory, you may need to worry.
Otherwise, you may save a few bytes, but you may pay for it in slower
access time.

Only optimize if you absolutely need the space or the time, and then
only attack the areas with the biggest payoffs until you fit into your
budget.

> Then, some day if the language supports [arrays of bits], you will
> be able to upgrade painlessly.

Can't happen.  "sizeof (char) == 1" is now fixed in the language, as
is the fact that it yields an integral type.
--
Tim McDaniel                 Applied Dynamics Int'l.; Ann Arbor, Michigan, USA
Work phone: +1 313 973 1300                        Home phone: +1 313 677 4386
Internet: mcdaniel at adi.com                UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.lang.c mailing list