简单的C枚举问题
我最近刚刚开始使用 C,有人要求我回答一些编码练习,其中出现以下代码:
typedef enum {
false = 0,
true = 1
} Bool;
有人可以对此提供一个简短而清晰的解释吗?
非常感谢。
I have just started C very recently and I have been asked to answer some coding exercises in which the following piece of code appears:
typedef enum {
false = 0,
true = 1
} Bool;
Could someone please provide a brief and clear explanation to that?
Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它实际上做了两件事;您可以将其分解为如下所示:
并且:
此代码创建一个新的枚举类型,然后使用
typedef
为其指定一个方便的名称。它允许您在代码中的其他位置使用名为Bool
的新“类型”,并为其分配值false
和true
。这是一个简单的用例:It's really doing two things; you can break it down something like this:
And:
This code creates a new enumeration type and then uses
typedef
to give it a convenient name. It would let you use a new 'type' calledBool
elsewhere in your code, and assign it the valuesfalse
andtrue
. Here's a simple use case:它只是枚举的定义,枚举是一种只能假定离散数量的值的类型,即括在这些括号中的值。每个值都有一个名称,您稍后可以使用该名称来引用它。如果您仅指定值的名称而不是实际值,编译器将按升序为您设置它们,从第一个元素的零开始。
请参阅有关枚举类型(特别是其 C 部分)了解更多信息。
该特定枚举定义了一个布尔类型,即只能采用两个值的类型:true 和 false,其中 false=!true。布尔值在编程中经常使用,例如作为标志来指示是否满足条件,实际上许多语言将它们作为本机类型包含(例如,C++ 和 C99 就是这样做的)。
顺便说一句,定义该枚举:这样
就足够了;然而,由于 C 被设计为使用此代码声明 Bool 类型的变量,因此您需要始终在 Bool 之前放置 enum 关键字:
使用 typedef 技巧,您可以定义一个匿名枚举以这种方式制作,然后为它提供一个名为 Bool 的别名;这样你就可以简单地执行以下操作:
It's just a definition of an enum, a type that can assume only a discrete number of values, i.e. the ones enclosed in these brackets. Each of these values is given a name, that you can later use to refer to it. If you only specify the name of the values and not the actual value, the compiler will set them for you in increasing order, starting from zero for the first element.
See the wiki article about enumerated types (and in particular its C section) for more information.
That specific enum defines a boolean type, i.e. a type that can assume only two values: true and false, where false=!true. The boolean values are used very often in programming, for example as flags to indicate if a condition is met, and actually many languages include them as a native type (C++ and C99, for example, do that).
By the way, to define that enum this:
would be enough; however, because of how C was designed to declare a variable of the Bool type with this code you would need to put always the enum keyword before Bool:
Using the typedef trick, instead, you define an anonymous enum made in that way, and then you provide an alias to it named Bool; in that way you can simply do:
由于 C 没有布尔数据类型,因此您的代码通过使用 typedef 来模拟布尔数据类型。
您可以使用新的用户定义数据类型,如下所示:
Since C does not have a Boolean data type, your code simulates one by making use of typedef.
You can make use of the new user defined data type as follows:
它提供了 true 和 false 的可读文字。但你可以猜到。
它是如何运作的?枚举(关键字
enum
)将一系列具有整数值的标记连接起来,并且typedef ... Bool;
使Bool
成为该类型的类型名。枚举。总的来说,我不鼓励这种习惯用法,因为您稍后可能会想使用
,并且如果
flag
设置为 2,这将不会达到您的预期。如果你长时间使用 c ,那么零是假的,其他一切都是真的,解释将成为第二天性。
It provides readable literals for true and false. But you could guess that.
How does it work? An enumeration (keyword
enum
) connects a series of tokens with integer values, and thetypedef ... Bool;
makesBool
the typename of the enumeration.On the whole I would discourage this idiom because you might later be tempted to use
and if
flag
got set to 2 this will not do what you expect.If you use c for long the zero is false everything else is true interpretation will beome second nature.
枚举是具有有限数量的符号值的整数类型。这允许您执行以下操作:
A enum is an integral type that has a limited number of symbolic values. This allows you do things like:
据我所知:
这声明了一个枚举,然后将名称 Bool 与该枚举关联起来(通过 typedef)。
您可以在此处获取有关 C 枚举的更多信息
From what I remember:
This declares an enumeration and then associates a name, Bool, to this enumeration (through typedef) .
You can get more information about C enumerations here