Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

bool factors[1000]
void main()
{
}
like image 877
Khushboo Avatar asked Aug 12 '10 20:08

Khushboo


People also ask

Can we declare bool in C?

Syntax to Declare Boolean Data Types in C:To declare a boolean data type in C we have to use a keyword named bool followed by a variable name. bool var_name; Here, bool is the keyword denoting the data-type and var_name is the variable name. A bool takes in real 1 bit, as we need only 2 different values(0 or 1).

Where is bool defined in Linux?

bool is just a macro that expands to _Bool . You can use _Bool with no #include very much like you can use int or double ; it is a C99 keyword. The macro is defined in <stdbool. h> along with 3 other macros.

How is bool defined in C?

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, '0' represents false value, while '1' represents true value. In C Boolean, '0' is stored as 0, and another integer is stored as 1.

Which library has bool in C?

The C programming language, as of C99, supports Boolean arithmetic with the built-in type _Bool (see _Bool). When the header <stdbool. h> is included, the Boolean type is also accessible as bool .


3 Answers

You simply need #include <stdbool.h>.

like image 57
Potatoswatter Avatar answered Oct 20 '22 10:10

Potatoswatter


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

like image 5
Michael Kristofik Avatar answered Oct 20 '22 09:10

Michael Kristofik


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

typedef enum {false,true} bool;
like image 2
user411313 Avatar answered Oct 20 '22 08:10

user411313