C语言 在 linux 平台上的 c 中声明 bool 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3471814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring bool variable in c on linux platform
提问by Khushboo
How to declare a variable of bool datatype in C running on Linux platform. I tried the following but its giving an error:
如何在 Linux 平台上运行的 C 中声明 bool 数据类型的变量。我尝试了以下但它给出了一个错误:
#include<stdio.h>
#include<string.h>
bool factors[1000]
void main()
{
}
回答by Potatoswatter
You simply need #include <stdbool.h>.
您只需要#include <stdbool.h>.
回答by Michael Kristofik
C doesn't have a booltype. You could use intinstead, using 0 for falseand 1 for true.
C 没有bool类型。您可以int改为使用 0 forfalse和 1 for true。
回答by user411313
If a type is not defined in your environment, you can define own types, also bool, e.g.
如果您的环境中没有定义类型,您可以定义自己的类型,也可以是 bool,例如
typedef enum {false,true} bool;
回答by david
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.
对于 bool 而言,unsigned char 通常是比 int 更好的选择,尤其是当您要拥有 1000 个数组时。尽管它的实现取决于无符号字符的大小以及数组的打包方式。
回答by mp.
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.
在 C99 中有一个 bool 类型。但我想知道为什么你不能用 C++ 编写代码。您不需要使用 C++ 的所有高级 OOP 功能。您可以编写“C 风格”代码并使用 C++ 编译器进行编译。

