枚举有多大? [英] How big is an enum?

查看:64
本文介绍了枚举有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在这里有点懒,但是...

我写了一个快速程序,我编译并运行在FreeBSD和Linux下

使用GCC和在Windows上使用Visual C版本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));

}

===================== ============================= ===================== ========

给出了这个输出


================== ================================ ================== ===========

enum 4 long 4,int 4 char 1

============== ==================================== ============== ===============


在所有系统上。


然而,仍然存在一个问题,即C ++标准声称要什么,b $ b应该是什么?是这样的吗?


我已经完成了这个标准,让我自己迷失在通常的

gobble dee gook里面。


帮助,有人知道对此的最终答案吗?


早先提出索赔我现在必须承认我不记得了

从英特尔编译器,VAX编译器和Sun

编译器的日子开始,如果真的如此吗?


干杯,

Pep。

解决方案



Pep写道:

还有仍然是一个问题,但C ++标准声称应该是什么情况?




7.2 - 5:枚举的基础类型是积分类型

,可以代表所有枚举值。它是实现定义的整数

类型用作枚举的基础类型,除非

基础类型不应大于int,除非
枚举器无法放入int或unsigned int。


Neelesh Bodas写道:


Pep写道:

但是仍然存在一个问题,那就是C ++标准声称应该是什么情况?



7.2 - 5:枚举的基础类型是一个整数类型
,它可以表示枚举中定义的所有枚举器值。它是实现定义的整数
类型用作枚举的基础类型,除了
基础类型不应大于int,除非
枚举器的值不能适合int或者unsigned int。




这导致了下一个问题。是一个长的大小的int,它在我的测试中肯定会出现吗?


干杯,

Pep 。


Pep写道:

这导致了下一个问题。这是一个很长的int,它在我的测试中肯定会出现吗?




它可能是你的实现。标准保证

它不会小于16位,但允许更大。


Kristo


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.

解决方案


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


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.


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


这篇关于枚举有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆