枚举问题。 [英] enum question.

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

问题描述

考虑以下代码:


#include< stdio.h>


#define SOMEVAL 1234

enum tree_types {PINE = 10,BIRCH,LARCH,OAK = 100,MAPLE,ELM,WILLOW};


void print_val(enum tree_types tree)

{

printf(树是:%d \ n,树);

}


int main ()

{

enum tree_types trees;

trees = PINE;

print_val(树);

trees = OAK;

print_val(树);

树= ELM;

print_val(树);

trees = SOMEVAL;

print_val(树);

返回0;

}

为什么允许使用SOMEVAL的assignmnt?我认为枚举类型只允许在枚举列表中指定值的
。为什么编译器

甚至没有警告它?我错了吗?


谢谢。


-

电子邮件:句柄,(点分隔),在gmail dot com。

解决方案

At_sea_with_C写道:


请考虑以下代码:


#include< stdio.h>


#define SOMEVAL 1234

enum tree_types {PINE = 10 ,BIRCH,LARCH,OAK = 100,MAPLE,ELM,WILLOW};


void print_val(enum tree_types tree)

{

printf(" tree is:%d \ n",tree);

}


int main()

{

enum tree_types trees;

trees = PINE;

print_val(树);

trees = OAK;

print_val(树);

trees = ELM;

print_val(树);

trees = SOMEVAL;

print_val(树);

返回0;

}


为什么是允许使用SOMEVAL的assignmnt?我认为枚举类型只允许在枚举列表中指定值的
。为什么编译器

甚至没有警告它?我错了吗?



这是允许的,因为你的想法是什么是错的。在C中,

enum只是某种整数的伪装(编译器

选择哪种),加上一些命名常量的引入

(所有类型`int'',不接受替代)。


这个自由的一个半实用的应用是当enum

值时不仅仅是任意数字代码,而是可以以有意义的方式组合




枚举CarStatus {

ALL_GREEN = 0x0,

DOOR_OPEN = 0x1,

BELTS_FASTENED = 0x2,

HEADLIGHTS_ON = 0x4

}状态;

...

status = DOOR_OPEN | HEADLIGHTS_ON;


结果是C'的枚举主要用于文档,

不用于执行。此外,一些调试器能够将

枚举值转换为名称;在你的例子中,调试器可能是

能够显示PINE而不是10。


-

Eric Sosman
es*****@acm-dot-org.inva 盖子


3月10日上午8:24,At_sea_with_C< blindal ... @ dev.null.invalidwrote:


(剪掉)


我认为枚举类型只允许在枚举列表中指定值的
。为什么编译器

甚至没有警告它?我错了吗?


谢谢。



摘自C标准草案(n869.pdf):


"附件I

(资料性)

常见警告

1实施可能会产生警告在许多情况下,

这些都没有被指定为本国际标准的一部分。

以下是一些比较常见的情况。

....


通过赋值枚举常量给予枚举类型除

之外的对象的值

类型的成员,

或具有相同类型的枚举变量,

或返回相同的函数的值枚举类型

(6.7.2.2)"

所以,我想一些实现可能会产生你预期的警告类型,但是

它不是必需的。


-

希望这有帮助,

Ste ven


At_sea_with_C写道:


请考虑以下代码:


#include< stdio.h>


#define SOMEVAL 1234

enum tree_types {PINE = 10,BIRCH,LARCH,OAK = 100,MAPLE,ELM,WILLOW};


void print_val(enum tree_types tree)

{

printf("树是:%d \ n",树);

}


int main()

{

enum tree_types trees;

trees = PINE;

print_val(树);

trees = OAK;

print_val(树);

trees = ELM;

print_val(树);

trees = SOMEVAL;

print_val(树);

返回0;

}


为什么允许使用SOMEVAL的assignmnt?我认为枚举类型只允许在枚举列表中指定值的
。为什么编译器

甚至没有警告它?我错了吗?



因为C'的枚举被破坏了。也许你在想C ++哪里

这是非法的?


-

Ian Collins。


Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

Thanks.

--
Email: The handle, (dot seperated), at gmail dot com.

解决方案

At_sea_with_C wrote:

Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

It''s allowed because "what you thought" is wrong. In C, an
enum is merely a disguise for some kind of integer (the compiler
chooses which kind), plus the introduction of some named constants
(all of type `int'', accept no substitutes).

One semi-useful application of this freedom is when the enum
values aren''t just arbitrary numeric codes, but can be combined
in meaningful ways:

enum CarStatus {
ALL_GREEN = 0x0,
DOOR_OPEN = 0x1,
BELTS_FASTENED = 0x2,
HEADLIGHTS_ON = 0x4
} status;
...
status = DOOR_OPEN | HEADLIGHTS_ON;

The upshot is that C''s enum is mostly useful for documentation,
not for enforcement. Also, some debuggers are able to convert
enum values to the names; in your example a debugger might be
able to show you PINE instead of 10.

--
Eric Sosman
es*****@acm-dot-org.invalid


On Mar 10, 8:24 am, At_sea_with_C <blindal...@dev.null.invalidwrote:

(snipped)

I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

Thanks.


An excerpt from a draft of the C standard (n869.pdf):

"Annex I
(informative)
Common warnings
1 An implementation may generate warnings in many situations,
none of which are specified as part of this International Standard.
The following are a few of the more common situations.

....

A value is given to an object of an enumeration type other than
by assignment of an enumeration constant that is a member of that
type,
or an enumeration variable that has the same type,
or the value of a function that returns the same enumeration type
(6.7.2.2)"
So, I suppose some implementations may
generate the type of warning you had expected, but
it''s not required.

--
Hope this helps,
Steven


At_sea_with_C wrote:

Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

Because C''s enums are broken. Maybe you where thinking of C++ where
this is illegal?

--
Ian Collins.


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

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