枚举数据类型

发布于 2024-10-03 12:54:38 字数 61 浏览 0 评论 0原文

enum 关键字有什么用?我们可以在哪里使用枚举数据类型? 谁能用例子解释一下吗?

What is the use of the enum keyword ? Where can we use enumerated data-type?
Can anyone explain with an example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把昨日还给我 2024-10-10 12:54:38

enum 为数字提供名称。

enum { MAX_LINES = 60 };

从表面上看,#define 也是如此:

#define MAX_LINES 60

enum 相对于 #define 的好处是名称会记录在调试信息中,因此调试器可以使用它。

您还可以使用枚举来定义一组相关值:

typedef enum { RED, GREEN, BLUE } Colour;

然后您可以使用这些值来索引 RGB 值的三元组数组:

typedef unsigned char Pixel[3];

Pixel p;

p[RED]   = 128;
p[GREEN] = 128;
p[BLUE]  = 255;

可能不是世界上最好的示例,但可以指示可以使用enum

An enum gives a name to a number.

enum { MAX_LINES = 60 };

Superficially, so does a #define:

#define MAX_LINES 60

The benefit of an enum over a #define is that the name is recorded in the debugging information, so the debugger can use it.

You can also use an enum to define a set of related values:

typedef enum { RED, GREEN, BLUE } Colour;

You might then use these to index into a triplet array for the RGB values:

typedef unsigned char Pixel[3];

Pixel p;

p[RED]   = 128;
p[GREEN] = 128;
p[BLUE]  = 255;

Probably not the world's best example, but an indication of where an enum can be used.

与风相奔跑 2024-10-10 12:54:38

枚举是始终被视为整数的枚举器(好吧,很简单),它们用于排列稍后可以使用的内容,例如通用值或索引。但是,正如名称所示,它们是枚举的,总是升序,例如:

enum myenum {
一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月

然后,如果您有一个数组,请说:

int months[12][30];

可以使用以下方式调用您的月份:

months[March][...];

另外,如果您声明:

enum myenum {  January = 1, February, March, ..., };

这您的程序将从 1 开始计数,依此类推。

最后,关于最后一条规则:您可以根据需要设置初始值,例如 10、20、30、42 等,但您也可以设置其中一个枚举值,并让其他值默认排序。

Enums are enumerators (ok, trivial) treated always as integers, and they are used to put in order something you could use later, like common values or indexes. But, as the name says, they are enumerated, always ascending, like:

enum myenum {
January, February, March, April, May, June, July, August, September, October, November, December
};

Then, if you have an array, say:

int months[12][30];

You can call your months using:

months[March][...];

Also, if you declare:

enum myenum {  January = 1, February, March, ..., };

This your program will start the counter of the enumeration from 1 and so on.

Finally, about this last rule: you can set initial values as you want, e.g., 10, 20, 30, 42 etc., but you can also set one of the enumerated values and let others be sorted by default.

森林很绿却致人迷途 2024-10-10 12:54:38

枚举是在“ANSI C”(或c89)时引入的,基本语法是

enum type {
 item1,
 item2,
 item3
};

它从0开始取值,item1将保存0,item2=1,item3=2..直到你告诉编译器它应该是什么值从开始,

enum type {
 item1 = 10,
 item2,
 item3
};

那么值是,item1=10,item2=11,item=12..编译器也会将它们视为整数。

int i=10;
i += item1;

i = i + 10;

您还可以定义枚举类型的变量

enum type mytype;
mytype = item1;

在这种情况下,变量 item1 被转换(静默)为类型“enum type”。您可以将“枚举类型”类型的变量与整数混合使用。

mytype = i;
mytype = mytype + 100;

尽管变量 mytype 的类型为“枚举类型”,但它也被视为整数,因此它可以保存任何整数变量。
但在我看来,枚举应该用作常量,不应该参与算术运算,并让编译器自行决定值而不是赋值。

enums are introduced at the time of "ANSI C" (or c89) the basic syntax is

enum type {
 item1,
 item2,
 item3
};

It takes value starting from 0, item1 will hold 0, item2=1, item3=2.. Until if you tell the compiler what value it should start from,

enum type {
 item1 = 10,
 item2,
 item3
};

then the values are, item1=10, item2=11, item=12.. Also the compiler will consider them as integers.

int i=10;
i += item1;

which is

i = i + 10;

You can also define a variable of the type enum.

enum type mytype;
mytype = item1;

In this case the variable item1 is converted (silently) to the type "enum type". You can mix the variable of type "enum type" with integer.

mytype = i;
mytype = mytype + 100;

though the variable mytype is of type "enum type" its also considered as integer hence it can hold any integer variable.
But IMO, enums should be used as constants and should not be involved in arithmetic operation and leave the compiler to decided the value on itself rather than assigning a value.

つ低調成傷 2024-10-10 12:54:38

枚举是常量整数值的列表,如

enum boolean { FALSE, TRUE };

枚举中的第一个名称的值为 0,下一个为 1,依此类推,除非指定了显式值。如果未指定所有值,则未指定的值将从最后指定的值继续进行:

enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

由于 char 在内部使用整数表示,因此您也可以声明指向字符常量的枚举

enum whitespaces { SPACE = ' ', TAB = ’\t’, NEWLINE = ’\n’, RETURN = ’\r’ };

#define 上的枚举可以为您生成值。此外,调试器能够以符号形式打印枚举变量的值。

An enumeration is a list of constant integer values, as in

enum boolean { FALSE, TRUE };

The first name in an enum has value 0, the next 1, and so on, unless explicit values are specified. If not all values are specified, unspecified values continue the progression from the last specified value:

enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

As a char is internally represented using an integer, you can declare enumerations that point to character constants as well:

enum whitespaces { SPACE = ' ', TAB = ’\t’, NEWLINE = ’\n’, RETURN = ’\r’ };

An advantage of enumeration over #define is that the values can be generated for you. In addition, a debugger may be able to print values of enumeration variables in their symbolic form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文