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()
{
}
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()
{
}
C doesn't have a bool
type. You could use int
instead, using 0 for false
and 1 for true
.
factors
main()
return int instead of void
If you want to use the name bool
, you can use a typedef such as the following. And possibly define TRUE and FALSE (although various include files sometimes define those already):
typedef int bool;
#define TRUE 1
#define FALSE 0
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.
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.