views:

214

answers:

7

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()
{
}
+2  A: 

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

Kristo
I thought C99 was C.
Fred Larson
C99 does have bool. #include <stdbool.h>
ninjalj
Yes, C DOES have a bool type. It's _Bool, and can be used by doing #include <stdbool.h>. Remember, C99 _IS_ C.
Clark Gaebel
@Clark Gaebel: nop, it's _Bool, and if you include stdbool.h you get a macro called bool.
ninjalj
I typed _Bool... it interpreted the underscore as markdown and made my thing italics =/
Clark Gaebel
@Clark: So escape it with backticks `_Bool`
caf
A: 
  • add a semicolon after the declaration of factors
  • make main() return int instead of void
  • compile it as C++
Andre Holzner
I tried with both of these but it still gives error.
Khushboo
Why not tell us what the error is? We're only guessing unless we know exactly what the compiler reported.
Marc Bernier
sorry, I did not realize that you wanted to compile this as C (instead of C++)
Andre Holzner
+9  A: 

You simply need #include <stdbool.h>.

Potatoswatter
Thank you so much !! It really helped
Khushboo
+1  A: 

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
Mark Wilkins
That could conflict with `#include <stdbool.h>`. Defining symbols used in the standard library is pretty risky.
Potatoswatter
I'd prefer all uppercase or all lowercase though. Either go `BOOL TRUE FALSE` like the Win32 APIs or `bool true false` like C++.
casablanca
+1  A: 

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.

mp
+1. Sensible answer. There is little a C compiler can do that won't be accepted by a C++ compiler, the C++ compiler has neat features on top of C (like, hey, bool), and is stricter on type safety than its C counterpart. And it could be a good way to learn some neat C++ tricks (like having a C struct initialize itself via a constructor...)
paercebal
C++ is not a substitute for C. See => variable length arrays, variadic macros, snprintf, designated initializes.
Clark Gaebel
I won't downvote because you're new, but generally you shouldn't suggest someone change their language, even from C to C++.
Potatoswatter
I think common sense should be valid also here at stack overflow. Nowadays there isn't any reason to continue to write code in Ansi C, unless very limited and specic cases (mantain legacy code, embedded systems etc...). I thought that suggesting Khushboo (who isn't a very experienced programmer) to consider the possibility of write his code in C++ wasn't a bad idea. I'm sorry if I was wrong.
mp
+1  A: 

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.

No. It's usually slower, and never faster.
Clark Gaebel
You're correct that access speed will generally be a tad slower, but the importance of access speed depends on how often and in what way the array is accessed. The memory savings may be far more important. Whether you're CPU constrained or memory constrained is something to consider. My guess is that it is more likely that the difference in access speed will not be noticed but the extra memory taken up by the array will be noticed. It all depends on your use scenario.
A: 

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

typedef enum {false,true} bool;