在linux平台上用c声明bool变量

发布于 2024-09-14 14:33:20 字数 171 浏览 5 评论 0原文

如何在 Linux 平台上运行的 C 语言中声明 bool 数据类型的变量。我尝试了以下操作,但出现错误:

#include<stdio.h>
#include<string.h>

bool factors[1000]
void main()
{
}

How to declare a variable of bool datatype in C running on Linux platform. I tried the following but its giving an error:

#include<stdio.h>
#include<string.h>

bool factors[1000]
void main()
{
}

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

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

发布评论

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

评论(5

冷情妓 2024-09-21 14:33:20

您只需要#include

You simply need #include <stdbool.h>.

傲世九天 2024-09-21 14:33:20

C 没有 bool 类型。您可以使用 int 来代替,使用 0 表示 false,使用 1 表示 true

C doesn't have a bool type. You could use int instead, using 0 for false and 1 for true.

挽你眉间 2024-09-21 14:33:20

如果您的环境中未定义类型,您可以定义自己的类型,也可以定义 bool,例如

typedef enum {false,true} bool;

If a type is not defined in your environment, you can define own types, also bool, e.g.

typedef enum {false,true} bool;
剩余の解释 2024-09-21 14:33:20

对于 bool 类型来说,unsigned char 通常是比 int 更好的选择,特别是如果您要拥有一个包含 1000 个此类类型的数组。尽管它的实现取决于无符号字符的大小以及数组的打包方式。

unsigned char is generally a better choice for a bool than an int, particularly if you are going to have an array of 1000 of them. Though it implementation dependent how large an unsigned char is and how the array will be packed.

海拔太高太耀眼 2024-09-21 14:33:20

C99 中有一个 bool 类型。但我想知道为什么你不能用 C++ 编写代码。您不需要使用 C++ 的所有高级 OOP 功能。您可以编写“C 风格”代码并使用 C++ 编译器对其进行编译。

In C99 there is a bool type. But I wonder why you can't write your code in C++. You don't need to use all the advanced OOP features of C++. You can write "C style" code and compiling it with a C++ compiler.

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