开发者

Declaring bool variable in c on linux platform

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>

boo开发者_运维知识库l factors[1000]
void main()
{
}


You simply need #include <stdbool.h>.


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


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

typedef enum {false,true} bool;


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜