我们可以定义没有值的布尔值吗?

发布于 2025-01-14 05:55:19 字数 161 浏览 3 评论 0原文

我们可以定义没有值的布尔值吗?

输入图片此处描述

Can we define boolean without a value?

enter image description here

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

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

发布评论

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

评论(3

仙女山的月亮 2025-01-21 05:55:19

要在 C 中使用布尔变量,您需要在标头中添加 #include

To use Boolean variables in C you need to #include <stdbool.h> in your headers.

心在旅行 2025-01-21 05:55:19

您可以定义一个没有值的布尔变量,如以下代码所示(但请注意第二个标头包含,请参阅下面的解释):

#include <stdio.h>
#include <stdbool.h>
int main (void) {
    bool u, t = true, f = false;

    if (t) puts("t is true"); else puts("t is false");
    if (f) puts("f is true"); else puts("f is false");
    if (u) puts("u is true"); else puts("u is false");

    return 0;
}

此输出(对我而言):

t is true
f is false
u is false

但是,您应该了解有关的正常规则在没有明确说明的情况下初始化变量。在给出的代码中,u 将保留一些任意值,事实上,使用更高警告级别的 gcc 可以清楚地表明这一点:

prog.c: In function ‘main’:
prog.c:8:8: warning: ‘u’ is used uninitialized in this function [-Wuninitialized]
    8 |     if (u) puts("u is true"); else puts("u is false");
      |        ^

请注意,如果您想使用 booltruefalse,这些不是关键字,而是已定义的关键字在 stdbool 头文件中,因此您需要包含它。

否则,您得到的只是 _Bool 类型(1),并且没有任何好的常量,因此您必须自己定义一些或使用 01 (尽管任何非零值都会转换为 1)。

由于这几乎不是使用 int 执行相同操作的一个步骤,因此您可能希望正确执行此操作(即使用标头)。


进一步注意,这仅在 C99 中可用。代码中存在 #include可能表明您正在使用过时的 C 实现(它是 DOS 的常见标头) -era 编译器,例如 Turbo C)。

如果是这种情况,很可能您甚至没有可以使用的 stdbool 标头,因此您必须坚持使用包含 int01

或者,您可以创建自己的 my_stdbool.h ,如下所示,假设没有前面提到的命名冲突(我见过的定义常量的大多数代码倾向于使用大写) case FALSETRUE 与常用准则保持一致):

#ifndef XYZZY_STDBOOL_H_INCLUDED
    #define XYZZY_STD_BOOL_H_INCLUDED
    typedef bool int;
    #define false 0
    #define true  1
#endif

(1) 丑陋,是的,但它确保了代码之间不会发生冲突可能已被写入在这成为标准的一部分之前,因为以 _X 开头的名称(其中 X 是任何大写字母或另一个 _)被保留给执行。因此你不应该使用它们。

You can very much define a boolean variable without a value, as evidenced by the following code (but note the second header include, see below for explanation):

#include <stdio.h>
#include <stdbool.h>
int main (void) {
    bool u, t = true, f = false;

    if (t) puts("t is true"); else puts("t is false");
    if (f) puts("f is true"); else puts("f is false");
    if (u) puts("u is true"); else puts("u is false");

    return 0;
}

This outputs (for me):

t is true
f is false
u is false

However, you should be aware of the normal rules regarding initialisation of variables where you don't make it explicit. In the code given, u will hold some arbitrary value and, indeed, using a higher warning level of gcc makes that clear:

prog.c: In function ‘main’:
prog.c:8:8: warning: ‘u’ is used uninitialized in this function [-Wuninitialized]
    8 |     if (u) puts("u is true"); else puts("u is false");
      |        ^

Note that, if you want to use bool, true, and false, these are not keywords, they're instead defined in the stdbool header file so you need to include that.

Otherwise all you get is the _Bool type(1) and neither of the nice constants, so you'll have to define some yourself or use 0 and 1 (although any non-zero value converts to 1).

Since this is scarcely a step up for doing the same thing with int, you probably want to do it properly (i.e., use the header).


Note further that this is only available as of C99. The presence of an #include <conio.h> in your code may indicate that you're using a hideously outdated implementation of C (it was a common header for DOS-era compilers such as Turbo C).

If that is the case, it's likely you don't even have a stdbool header that you can use, so you'll have to stick with int, 0, and 1.

Alternatively, you may be able to create your own my_stdbool.h such as the following, assuming there's no naming conflict as previously mentioned (most code I've seen that defines the constants tends to use the upper-case FALSE and TRUE in keeping with commonly-used guidelines):

#ifndef XYZZY_STDBOOL_H_INCLUDED
    #define XYZZY_STD_BOOL_H_INCLUDED
    typedef bool int;
    #define false 0
    #define true  1
#endif

(1) Uglier, yes, but it ensures there's no clash between code that may have been written before this became part of the standard since names beginning with _X (where X is any upper-case letter or another _) are reserved for the implementation. Hence you shouldn't be using them.

停滞 2025-01-21 05:55:19

在 C 中,您可以使用 _Bool 类型声明布尔变量。以下为例。但是,这不是推荐的方法。

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    _Bool k=0, l = 1, m = 324234;
    printf("%d\n", k);
    printf("%d\n", l);
    printf("%d\n", m);
    return 0;
}

您可以进一步使用宏将_Bool定义为bool

#include <stdio.h>
typedef _Bool bool;
int main()
{
    printf("Hello, World!\n");
    bool k=0, l = 1, m = 324234;
    printf("%d\n", k);
    printf("%d\n", l);
    printf("%d\n", m);
    return 0;
}

In C, you can declare a boolean variable using the _Bool type. Following as an example. However, it is not a recommended method.

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    _Bool k=0, l = 1, m = 324234;
    printf("%d\n", k);
    printf("%d\n", l);
    printf("%d\n", m);
    return 0;
}

You can further use a macro to define _Bool as bool.

#include <stdio.h>
typedef _Bool bool;
int main()
{
    printf("Hello, World!\n");
    bool k=0, l = 1, m = 324234;
    printf("%d\n", k);
    printf("%d\n", l);
    printf("%d\n", m);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文