By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,951 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,951 IT Pros & Developers. It's quick & easy.

How big is an enum?

P: n/a
Pep
Okay I am being slightly lazy here but ...

I wrote a quick program that I have compiled and run under FreeBSD and Linux
using GCC and on Windows using Visual C version 6

================================================== =============================
#include <stdio.h>

enum { A, B, C} aa;
long bb;
int cc;
char dd;

int main(int argc, char** argv)
{
printf("enum %d long %d, int %d char %d\n", sizeof(aa), sizeof(bb),
sizeof(cc), sizeof(dd));
}
================================================== =============================
which gives this output

================================================== =============================
enum 4 long 4, int 4 char 1
================================================== =============================

on all systems.

There still remains the question however as to what the C++ standard claims
should be the case?

I have waded through the standard and have got myself lost in the usual
gobble dee gook in there.

Help, anyone know the definitive answer to this?

Having made a earlier claim I must now admit that I do not remember
sufficiently from my days on the Intel compiler, the VAX compiler and Sun
compilers if this is really the case?

Cheers,
Pep.

Nov 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

Pep wrote:
There still remains the question however as to what the C++ standard claims
should be the case?


7.2 - 5 : "The underlying type of an enumeration is an integral type
that can represent all the enumerator values
defined in the enumeration. It is implementation defined which integral
type is used as the underlying type for an enumeration except that the
underlying type shall not be larger than int unless the value of an
enumerator cannot fit in an int or unsigned int."

Nov 22 '05 #2

P: n/a
Pep
Neelesh Bodas wrote:

Pep wrote:
There still remains the question however as to what the C++ standard
claims should be the case?


7.2 - 5 : "The underlying type of an enumeration is an integral type
that can represent all the enumerator values
defined in the enumeration. It is implementation defined which integral
type is used as the underlying type for an enumeration except that the
underlying type shall not be larger than int unless the value of an
enumerator cannot fit in an int or unsigned int."


Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

Cheers,
Pep.

Nov 22 '05 #3

P: n/a
Pep wrote:
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?


It might be for your implementation. The standard guarantees that
it'll be no smaller than 16 bits, but it is permitted to be larger.

Kristo

Nov 22 '05 #4

P: n/a
"Pep" <pe*@nowhere.com> wrote in message
news:dl**********@pop-news.nl.colt.net...
: Neelesh Bodas wrote:
:
: >
: > Pep wrote:
: >> There still remains the question however as to what the C++ standard
: >> claims should be the case?
: >>
: >
: > 7.2 - 5 : "The underlying type of an enumeration is an integral type
: > that can represent all the enumerator values
: > defined in the enumeration. It is implementation defined which
integral
: > type is used as the underlying type for an enumeration except that the
: > underlying type shall not be larger than int unless the value of an
: > enumerator cannot fit in an int or unsigned int."
:
: Which leads to the next question. Is a int the size of a long, which it
: certainly appears to be in my tests?

This does appear to be the case on your platform.

Yet, the compiler in your example could have elected to use a char
rather than an int to represent the enumeration. Some compilers
allow you to configure this behavior.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Nov 22 '05 #5

P: n/a
Pep wrote:
Neelesh Bodas wrote:
[snip]
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?


may be, standard only guarantees that size of long is not less than int.

Krishanu
Nov 22 '05 #6

P: n/a
Krishanu Debnath wrote:
Pep wrote:
Neelesh Bodas wrote:


[snip]
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?


may be, standard only guarantees that size of long is not less than int.

Krishanu


On my platform, long = 40 bits and int = 32 bits.

Cheers! --M

Nov 22 '05 #7

P: n/a
Pep <pe*@nowhere.com> wrote:
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?


In TC++PL:SE, p.75 (section 4.6):

(In the below, I use '===' to mean 'defined as')

1 === sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

1 <= sizeof(bool) <= sizeof(long)

sizeof(char) <= sizeof(wchar_t) <= sizeof(long)

sizeof(float) <= sizeof(double) <= sizeof(long double)

sizeof(N) === sizeof(signed N) === sizeof(unsigned N)

where N can be char, short int, int, or long int. In addition, it is
guaranteed that a char has at least 8 bits, a short and an int at
least 16 bits, and a long at least 32 bits.
So the way I interpret it, a conforming implementation is free to make

sizeof(char) == sizeof(long) == 1

for a big enough 'char'.

--
Marcus Kwok
Nov 22 '05 #8

P: n/a

Pep wrote:
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?


3.9.1 - 2: "There are four signed integer types: "signed char",
"short int", "int", and "long int." In this list, each type
provides at least as much storage as those preceding it in the list.
Plain ints have the natural size suggested by the architecture of the
execution environment) ; the other signed integer types are provided to
meet special needs."

Nov 22 '05 #9

P: n/a
mlimber wrote:
Krishanu Debnath wrote:
Pep wrote:
Neelesh Bodas wrote:

[snip]
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

may be, standard only guarantees that size of long is not less than int.

Krishanu


On my platform, long = 40 bits and int = 32 bits.

Cheers! --M


And how does this violates my assertion?

Krishanu
Nov 22 '05 #10

P: n/a
Krishanu Debnath wrote:
mlimber wrote:
Krishanu Debnath wrote:
Pep wrote:
Neelesh Bodas wrote:
[snip]

Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

may be, standard only guarantees that size of long is not less than int.

Krishanu


On my platform, long = 40 bits and int = 32 bits.

Cheers! --M


And how does this violates my assertion?

Krishanu


It doesn't. I was providing a concrete example confirming it.

Cheers! --M

Nov 22 '05 #11

P: n/a
Pep
mlimber wrote:
Krishanu Debnath wrote:
mlimber wrote:
> Krishanu Debnath wrote:
>> Pep wrote:
>>> Neelesh Bodas wrote:
>> [snip]
>>
>>> Which leads to the next question. Is a int the size of a long, which
>>> it certainly appears to be in my tests?
>>>
>> may be, standard only guarantees that size of long is not less than
>> int.
>>
>> Krishanu
>
> On my platform, long = 40 bits and int = 32 bits.
>
> Cheers! --M
>


And how does this violates my assertion?

Krishanu


It doesn't. I was providing a concrete example confirming it.

Cheers! --M


To everyone, especially Neelesh, thanks very much for the information :)
Nov 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.