C语言 我可以用枚举变量做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2502648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:55:12  来源:igfitidea点击:

What can I do with an enum variable?

cvariablesenumsdefinitiontypedef

提问by drigoSkalWalker

When I declare a enum variable like this:

当我像这样声明一个枚举变量时:

enum paint_colors { RED, GREEN, BLUE, ...} colors;

is the colorsvariable useful? If so, what can I do with it?

colors可变的有用吗?如果是这样,我可以用它做什么?

Thanks in advance.

提前致谢。

回答by Carl Norum

Really what you're doing there is declaring a variable inline with the rest of the definition of the enumeration. It's equivalent to:

实际上,您在那里所做的是声明一个与枚举定义的其余部分内联的变量。它相当于:

enum paint_colors { RED, GREEN, BLUE };
enum paint_colors colors;

Often, you'll see a typedefassociated with the definition:

通常,您会看到typedef与定义相关联的:

typedef enum _paint_colors { RED, GREEN, BLUE } paint_colors;

Which lets you use the enumeration more like the built in types:

这使您可以像使用内置类型一样使用枚举:

paint_colors color;

So the answer to your question is that colorsis a variable of type enum paint_colors, and can be used any way you think is appropriate for such a variable:

所以你的问题的答案是这colors是一个 type 的变量enum paint_colors,并且可以以任何你认为适合这样的变量的方式使用:

colors = RED;
if (colors != GREEN)
{
    colors = BLUE;
}

And so on.

等等。

回答by bta

Internally, an enumis treated as an integer that can only hold a limited range of values. In this case, the constants RED, GREEN, BLUE, ... will be defined and will be equal to 0, 1, 2, ... (respectively). The variable colorscan be used anywhere an intcan be used. You can use operators like ++to iterate through the list of colors. The only difference between declaring enum paint_colors colorsand int colorsis that the enumerated variable canshould onlybe assigned one of the enumerated constants.

在内部, anenum被视为只能容纳有限范围值的整数。在这种情况下,常量RED, GREEN, BLUE, ... 将被定义并且等于0, 1, 2, ... (分别)。该变量colors可以在任何可以使用的地方int使用。您可以使用运算符 like++遍历颜色列表。声明之间的唯一区别enum paint_colors colorsint colors是所列举的变量可以被分配枚举常数之一。

This gives you several benefits over using #defineto create a series of constants and using a normal intfor colors. First, some debuggers can detect that colorsis an enumerated type and will display the name of the enumerated constant instead of a numeric value.

#define与用于创建一系列常量和使用法线intfor 相比,这为您提供了几个好处colors。首先,一些调试器可以检测到它colors是一个枚举类型,并将显示枚举常量的名称而不是数字值。

More importantly, this can add an additional layer of type checking. It is not required by the C standard, but some compilers check and make sure that values assigned to a variable of enumerated type correspond to one of the enumerated constants.

更重要的是,这可以添加额外的类型检查层。C 标准不要求这样做,但一些编译器会检查并确保分配给枚举类型变量的值对应于枚举常量之一。

Mentally, you can almost think of this is similar to saying:

心理上,你几乎可以想到这类似于说:

#define RED    0
#define GREEN  1
#define BLUE   2
typedef int paint_colors;
paint_colors colors;

The variable is treated like an int, but explicitly giving it a different type helps to clarify what the variable is and what it is used for.

该变量被视为int,但明确赋予它不同的类型有助于阐明变量是什么以及它的用途。

回答by Jeremy Ruten

You could paint a roller coaster!

你可以画一个过山车!

typedef struct {
  Color car;
  Color door_handle;
  Color wheels;
} RollerCoasterColors;

int main(void) {
  RollerCoasterColors roller_coaster_colors;
  roller_coaster_colors.car = RED;
  roller_coaster_colors.door_handle = YELLOW;
  roller_coaster_colors.wheels = PURPLE;

  // now paint_roller_coaster() knows what colour to paint each part!
  paint_roller_coaster(roller_coaster_colors);
}

回答by user2978119

In C, the enum type variable can be iterated, such as ++, +; But this is not allowed in C++;

在C中,枚举类型的变量是可以迭代的,比如++、+;但这在 C++ 中是不允许的;

enum WEEKDAY {MON, TUE, WED, THU, FRI, SAT, SUN, END};
for(WEEKDAY day = 0; day < END; day++){
    printf("day index = %d\n", day);
}

can be compiled in C, but not in C++;

可以用C编译,但不能用C++编译;

回答by AMIT KUMAR

enum paint_colors { RED, GREEN, BLUE, ...} colors;

This is the enumeration type declaration you have made. Answering specifically to your question, you can declare an int variable as usual,

这是您所做的枚举类型声明。专门回答您的问题,您可以像往常一样声明一个 int 变量,

enum paint_colors { RED, GREEN, BLUE, ...};
int colors;

and perform all operations on colorsvariable same as you would have done with your declaration. It will not make any difference in operation part. Be it assigning a integer value outside the enumerators constant value,

并对颜色变量执行所有操作,就像您对声明所做的一样。它不会对操作部分产生任何影响。无论是在枚举常量值之外分配一个整数值,

colors=1676;    //Valid in both of our case

The only difference it makes is making the code more readable and better to understand because enum type variable indicates its purpose to the programmer(specially debugger). Depends on you ;)

它所做的唯一区别是使代码更具可读性和更好的理解,因为枚举类型变量向程序员(特别是调试器)表明了它的用途。取决于你 ;)

回答by Grant Palin

An enum is helpful when you have a given set of possible values, where each value has a meaningful name. Elsewhere, you refer to the enum values, rather than "magic numbers", which can become meaningless among other code.

当您有一组给定的可能值时,枚举很有用,其中每个值都有一个有意义的名称。在其他地方,您指的是枚举值,而不是“幻数”,这在其他代码中可能变得毫无意义。

回答by Jacek Konieczny

I find enums especially useful during debugging. If you just use #defineto define constants used as values for some variable the debugger will show you the numer. With enum it can show you the names.

我发现枚举在调试过程中特别有用。如果您只是#define用来定义用作某些变量值的常量,调试器将显示数字。使用 enum 它可以显示名称。

And if you need a set of possible values (constants) for a single variable you can define it the way you showed.

如果您需要单个变量的一组可能值(常量),您可以按照您显示的方式定义它。