473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How big is an enum?

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
11 2035

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
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
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
"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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.