枚举类型int或unsigned int? [英] enum type int or unsigned int?

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

问题描述

大家好,


编译器是否可以选择要签名或未签名的枚举类型

int?我认为它必须是int;看起来它会根据

分配的值而改变。下面如果我没有初始化FOO_STORE,比如-10,

我得到一个关于无符号比较的警告,我看到一个无限的

循环。


如果我初始化FOO_STORE = -10,我没有看到任何警告。没有

无限循环。


不是FOO_xyz需要成为''int'的类型吗?

编译器可以选择其类型为unsigned int吗?

--------

#include< stdio.h>


typedef enum {

FOO_STORE,/ *如果= -10,则不添加警告。工作正常* /

FOO_HASHTABLE,

FOO_STATE,

FOO_CONFIG,

FOO_MAX

} foo_subobj_type_en;

int main(void){

foo_subobj_type_en sub_obj;


for(sub_obj = FOO_MAX-1; sub_obj > = FOO_STORE; --sub_obj){

printf("%d \ n",sub_obj);

}

返回0;

}

Hi all,

Can the compiler chose the type of an enum to be signed or unsigned
int? I thought it must be int; looks like it changes based on the
assigned values. Below if I don''t initialize FOO_STORE to be, say -10,
I get a warning about unsigned comparison and I''m seeing an infinite
loop.

If I do initialize FOO_STORE = -10, I don''t see any warnings. No
infinite loop.

Isn''t the type of FOO_xyz required to be an ''int''?
Can the compiler choose its type to be unsigned int?

--------
#include <stdio.h>

typedef enum {
FOO_STORE , /* if = -10 is added, no warnings. works fine */
FOO_HASHTABLE,
FOO_STATE,
FOO_CONFIG,
FOO_MAX
} foo_subobj_type_en;
int main(void) {
foo_subobj_type_en sub_obj;

for (sub_obj = FOO_MAX-1; sub_obj >= FOO_STORE; --sub_obj) {
printf("%d\n", sub_obj);
}
return 0;
}

gcc --version
gcc(GCC)3.2.3 20030502(Red Hat Linux 3.2.3-52)

gcc -Wall -W -ansi -pedantic enum.c
enum.c:在函数`main''中:

enum.c:15:警告:无符号表达式> = 0的比较总是

true
gcc --version gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-52)
gcc -Wall -W -ansi -pedantic enum.c enum.c: In function `main'':
enum.c:15: warning: comparison of unsigned expression >= 0 is always
true




谢谢,

Karthik



Thanks,
Karthik

推荐答案

> foo_subobj_type_en sub_obj;


所以sub_obj的值范围是

FOO_STORE,

FOO_HASHTABLE,

FOO_STATE,

FOO_CONFIG,

FOO_MAX
> foo_subobj_type_en sub_obj;

So the value range of sub_obj is
FOO_STORE ,
FOO_HASHTABLE,
FOO_STATE,
FOO_CONFIG,
FOO_MAX
for(sub_obj = FOO_MAX-1; sub_obj> = FOO_STORE; --sub_obj )
for (sub_obj = FOO_MAX-1; sub_obj >= FOO_STORE; --sub_obj)



sub_obj将在FOO_STORE~FOO_MAX范围内。


sub_obj> = FOO_STORE始终为TRUE。这就是你收到警告的原因。

如果你将枚举类型转换为int,FOO_STORE~FOO_MAX将被视为

" int" 。

在这种情况下,您可以指定FOO_STORE =整数值,所有FOO_ *

成为CONST INT。


sub_obj will be in the range of FOO_STORE ~ FOO_MAX.

sub_obj >= FOO_STORE always is TRUE. That is why you get the warning.

If you cast enum type into "int", FOO_STORE~FOO_MAX will be treated as
"int".
In this case, you can assign FOO_STORE = integer value, all FOO_*
become CONST INT.


"ka*****@gmail.com" < KA ***** @ gmail.com>写道:
"ka*****@gmail.com" <ka*****@gmail.com> writes:
大家好,

编译器是否可以选择枚举类型进行签名或取消签名
int?我认为它必须是int;看起来它会根据
分配的值而改变。下面如果我没有初始化FOO_STORE,比如-10,
我收到一个关于无符号比较的警告,我看到一个无限的循环。

如果我初始化FOO_STORE = -10,我没有看到任何警告。没有
无限循环。

不是FOO_xyz的类型需要是''int''?
编译器可以选择它的类型为unsigned int吗? br />
--------
#include< stdio.h>

typedef enum {
FOO_STORE,/ * if = -10添加,没有警告。工作正常* /
FOO_HASHTABLE,
FOO_STATE,
FOO_CONFIG,
FOO_MAX
} foo_subobj_type_en;

int main(void){
foo_subobj_type_en sub_obj;

for(sub_obj = FOO_MAX-1; sub_obj> = FOO_STORE; --sub_obj){
printf("%d \ n",sub_obj);
}
返回0;
}
Hi all,

Can the compiler chose the type of an enum to be signed or unsigned
int? I thought it must be int; looks like it changes based on the
assigned values. Below if I don''t initialize FOO_STORE to be, say -10,
I get a warning about unsigned comparison and I''m seeing an infinite
loop.

If I do initialize FOO_STORE = -10, I don''t see any warnings. No
infinite loop.

Isn''t the type of FOO_xyz required to be an ''int''?
Can the compiler choose its type to be unsigned int?

--------
#include <stdio.h>

typedef enum {
FOO_STORE , /* if = -10 is added, no warnings. works fine */
FOO_HASHTABLE,
FOO_STATE,
FOO_CONFIG,
FOO_MAX
} foo_subobj_type_en;
int main(void) {
foo_subobj_type_en sub_obj;

for (sub_obj = FOO_MAX-1; sub_obj >= FOO_STORE; --sub_obj) {
printf("%d\n", sub_obj);
}
return 0;
}
gcc --version
gcc --version


gcc(GCC)3.2.3 20030502( Red Hat Linux 3.2.3-52)


gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-52)

gcc -Wall -W -ansi -pedantic enum.c
gcc -Wall -W -ansi -pedantic enum.c


enum.c:在函数`main'中':
enum.c:15:警告:无符号表达式的比较> = 0总是
true


enum.c: In function `main'':
enum.c:15: warning: comparison of unsigned expression >= 0 is always
true




允许这种行为在C99 ...但是因为你在C89模式中调用GCC

...


我的C89草稿副本非常清楚地说,枚举类型具有与int兼容的

类型。这种类型的对象必然会签署,除非我遗漏了什么。


C99允许实现选择与之兼容的类型

任何char,任何有符号整数类型或任何无符号整数

类型。常量/本身/,必须有int类型。


在我看来,GCC在这方面已经破了。


-

Micah J. Cowan

程序员,音乐家,排版爱好者,游戏玩家......
http://micah.cowan.name/


Micah Cowan< mi *** @ cowan.name>写道:

[...]
Micah Cowan <mi***@cowan.name> writes:
[...]
我的C89草稿非常清楚地表明枚举类型具有与int兼容的类型。这种类型的对象必须签名,除非我遗漏了什么。

C99允许实现选择与任何char兼容的类型,任何有符号整数类型,或任何无符号整数
类型。常量/本身/必须具有int类型。
My draft copy of C89 says very clearly that enumerated types have a
type compatible with int. An object of that type would necessarily be
signed, unless I''m missing something.

C99 allows the implementation to choose a type that is compatible with
any of char, any signed integer type, or any unsigned integer
type. The constants /themselves/, must have type int.




您的C89草案和实际标准之间是否有变化? (我想知道你是否混淆了enum文字的类型与enum类型本身的

兼容类型。)


根据我的ISO C90标准副本,第6.5.2.2节:


枚举器列表中的标识符被声明为常量

有类型int并且可能出现在允许的地方。


...


每个枚举类型应与整数类型兼容;

类型的选择是实现定义的。


C99 6.7.2.2说:


标识符枚举器列表被声明为常量

,其类型为int,并且可能出现在允许的地方。


...


每个枚举类型应与char,签名的

整数类型或无符号整数类型兼容。类型的选择是

实现定义,但应能够代表枚举的所有成员的

值。


几乎相同。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Did that change between your C89 draft and the actual standard? (I
wonder if you''re confusing the type of the enum literals with the
compatible type of the enum type itself.)

According to my copy of the ISO C90 standard, section 6.5.2.2:

The identitiers in an enumerator list are declared as constants
that have type int and may appear wherever such are permitted.

...

Each enumerated type shall be compatible with an integer type; the
choice of type is implementation-defined.

C99 6.7.2.2 says:

The identifiers in an enumerator list are declared as constants
that have type int and may appear wherever such are permitted.

...

Each enumerated type shall be compatible with char, a signed
integer type, or an unsigned integer type. The choice of type is
implementation-defined, but shall be capable of representing the
values of all the members of the enumeration.

which is nearly the same.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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