简单的C枚举问题

发布于 2024-08-22 19:48:49 字数 158 浏览 3 评论 0原文

我最近刚刚开始使用 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 技术交流群。

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

发布评论

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

评论(6

顾挽 2024-08-29 19:48:49

它实际上做了两件事;您可以将其分解为如下所示:

enum _bool {
   false = 0,
   true = 1
};

并且:

typedef enum _bool Bool;

此代码创建一个新的枚举类型,然后使用 typedef 为其指定一个方便的名称。它允许您在代码中的其他位置使用名为 Bool 的新“类型”,并为其分配值 falsetrue。这是一个简单的用例:

Bool logical_not(Bool in)
{
    if (in == true)
        return false;
    else
        return true;
}

It's really doing two things; you can break it down something like this:

enum _bool {
   false = 0,
   true = 1
};

And:

typedef enum _bool Bool;

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' called Bool elsewhere in your code, and assign it the values false and true. Here's a simple use case:

Bool logical_not(Bool in)
{
    if (in == true)
        return false;
    else
        return true;
}
素年丶 2024-08-29 19:48:49

它只是枚举的定义,枚举是一种只能假定离散数量的值的类型,即括在这些括号中的值。每个值都有一个名称,您稍后可以使用该名称来引用它。如果您仅指定值的名称而不是实际值,编译器将按升序为您设置它们,从第一个元素的零开始。

请参阅有关枚举类型(特别是其 C 部分)了解更多信息。

该特定枚举定义了一个布尔类型,即只能采用两个值的类型:true 和 false,其中 false=!true。布尔值在编程中经常使用,例如作为标志来指示是否满足条件,实际上许多语言将它们作为本机类型包含(例如,C++ 和 C99 就是这样做的)。

顺便说一句,定义该枚举:这样

enum Bool
{
    false = 0,
    true = 1
};

就足够了;然而,由于 C 被设计为使用此代码声明 Bool 类型的变量,因此您需要始终在 Bool 之前放置 enum 关键字:

enum Bool myFlag=true;

使用 typedef 技巧,您可以定义一个匿名枚举以这种方式制作,然后为它提供一个名为 Bool 的别名;这样你就可以简单地执行以下操作:

Bool myFlag=true;

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:

enum Bool
{
    false = 0,
    true = 1
};

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:

enum Bool myFlag=true;

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:

Bool myFlag=true;
如此安好 2024-08-29 19:48:49

由于 C 没有布尔数据类型,因此您的代码通过使用 typedef 来模拟布尔数据类型。

您可以使用新的用户定义数据类型,如下所示:

Bool find_key_in_array() {
        Bool found = false; // assume not found.

        // do searching and set found suitably.    

        return found;
}

int main()  {

        Bool result = find_key_in_array();

        return 0;
}

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:

Bool find_key_in_array() {
        Bool found = false; // assume not found.

        // do searching and set found suitably.    

        return found;
}

int main()  {

        Bool result = find_key_in_array();

        return 0;
}
灵芸 2024-08-29 19:48:49

它提供了 true 和 false 的可读文字。但你可以猜到。

它是如何运作的?枚举(关键字enum)将一系列具有整数值的标记连接起来,并且typedef ... Bool;使Bool成为该类型的类型名。枚举。

总的来说,我不鼓励这种习惯用法,因为您稍后可能会想使用

int flag=false;
// something happens that might change the setting of flag that *doesn't* use the enum
if (flag == true) {
   //...
}

,并且如果 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 the typedef ... Bool; makes Bool the typename of the enumeration.

On the whole I would discourage this idiom because you might later be tempted to use

int flag=false;
// something happens that might change the setting of flag that *doesn't* use the enum
if (flag == true) {
   //...
}

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.

孤千羽 2024-08-29 19:48:49

枚举是具有有限数量的符号值的整数类型。这允许您执行以下操作:

Bool finish = false;

while (finish != true)
{
    ...
}

A enum is an integral type that has a limited number of symbolic values. This allows you do things like:

Bool finish = false;

while (finish != true)
{
    ...
}
打小就很酷 2024-08-29 19:48:49

据我所知:

这声明了一个枚举,然后将名称 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

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