在linux平台上用c声明bool变量
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只需要
#include
。You simply need
#include <stdbool.h>
.C 没有
bool
类型。您可以使用int
来代替,使用 0 表示false
,使用 1 表示true
。C doesn't have a
bool
type. You could useint
instead, using 0 forfalse
and 1 fortrue
.如果您的环境中未定义类型,您可以定义自己的类型,也可以定义 bool,例如
If a type is not defined in your environment, you can define own types, also bool, e.g.
对于 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.
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.