tags:

views:

269

answers:

5

Is there any downside to using

typedef char bool;
enum boolean { false, true };

in C to provide a semantic boolean type?

+1  A: 

I think it's fine, nice and clean.

Scott Stafford
+9  A: 

In C99, you should be using stdbool.h, which defines bool, true, and false.

Otherwise what you have is fine. Using just the enum may be a bit simpler, but if you really want to save space what you have works.

GMan
Probably his platform has `sizeof(any_enum_t) == sizeof(int)`.
Potatoswatter
@Potato: Ah, right. I just don't think that may actually be worth it. Not really a loss though, so meh. (If I'm not mistaken, that particular enum must have the underlying type `int`, by the way.)
GMan
@GMan: My C is rusty, but I think the `int` requirement is a C++03-ism. Since the x86-64 ABI also says the same thing, there isn't likely to be much difference, though.
Potatoswatter
@Potato: Oh, right again. I should stop answering C questions, because I inevitably eventually think I'm answering a C++ question.
GMan
+3  A: 

The downside of typedef char bool; is that if you compile with a C99 implementation and happen to include <stdbool.h>, this ends up as typedef char _Bool;, which is wrong. Also, if you ever tried to compile the code as C++, you'd have issues (that's not necessarily a problem, but it could be).

It would probably better either to use <stdbool.h> if your implementation provides one or to use a different name for the type, like BOOL.

James McNellis
+4  A: 

The short answer is: it's fine. It's particularly good if you needed to make large arrays of them, although I would be tempted to just use the C99 built-in1.

Since you asked "is there any downside..." I suppose I could remark that there have been important machines that did not actually have a character load instruction. (The Cray and initial DEC Alpha come to mind.) Machines in the future may suddenly go all minimal once again.

It will always be fast to load a standard integral type.

It will probably always be fast to load a single character.


1. See C99 6.2.5. There is a built-in type _Bool. Then, if you include <stdbool.h> (see C99 7.16) you get an alias, the more gracefully named bool, and defines for true and false. If you use this it will clash with your typedef but I'm sure it would be an easy thing to fix.

DigitalRoss
A: 

I would suggest using a bit to represent true or false, rather than a character. A character uses 8 bits, We can set 1 for true and 0 for false with just 1 bit. That will be more memory efficient and also satisfies the purpose. (e.g) char flag:1;

Reference : http://en.wikipedia.org/wiki/Bit_field

sethu
Memory efficient doesn't necessarily mean better. Access to bits is likely to be quite slow, and unnecessarily complex.
GMan
Using Bool datatype is better, I agree. It is also 1 bit. But can you please explain why it is slow. I am a newbie !!
sethu
@GMan: if you need a large number of boolean values indexed by integer positions, a bit array is definitely the way to go. If it's just a single value, using `int` or C99 `bool` is perfectly fine. I don't see any reason to use `char`.
R..
@sethu: CPU's don't operate on bits, they operate on their native word size. To access a bit, you have to do bit manipulation, which is far less straightforward. I agree with @R though, while in the general case it's not worth it, if you're storing a large array of booleans than bits are probably the way to go.
GMan
Thanks , I understood !!
sethu