Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to define BOOL in my C code. But I am really struck up at the question of using one of the following.

typedef unsigned char BOOL;
typedef char BOOL;
typedef unsigned int BOOL;
typedef int BOOL;

I know in terms of space char would be a better option, but I need speed aswell.
Also I would really appreciate the usage of char is much more efficient in terms of space and execution when compared to that of int and also vice versa.

Please suggest.
Posted

int is the fastest because of how a CPU is implemented
found an answer here too :
http://stackoverflow.com/questions/9521140/char-or-int-for-boolean-value-in-c[^]
 
Share this answer
 
v2
Sign: Definitely unsigned because a true value must be always bigger than a false value by definition. If false is zero, then true shouldn't be negative.

Type: I think there is no good answer to this question. The answer is dependent on the architecture and the compiler in use plus a lot of other factors (where do you use it and how): for example in a bool array a char might be better because a cache line can hold more items if you iterate over the array, but on the other hand in a packed struct an int might be better because it wont screw up the alignment of the next member in the struct... And we could give similar pros and contras for each solution. The C++ bool is 1 byte, and the C winapi uses a BOOL that is defined as follows:
typedef long BOOL;

My answer: choose whatever width you like, but I would stick to unsigned for the reason I mentioned above.

You should consider switching to .cpp source files from .c if its possible. I'm not kidding. Even if you write your source in C style without object oriented constructs you get some benefits! You have a bool type immediately, can declare your variables anywhere inside a function, name mangling prevents linking of incompatible functions/function users, you can use C++ classes/smart pointers/containers even without serious C++ knowledge, ...
 
Share this answer
 
Comments
[no name] 1-Aug-12 16:08pm    
Is C++ faster than C ? Supposing logic is implemented in the best way in both the languages. i have this query since a long time :)
pasztorpisti 1-Aug-12 16:15pm    
Not faster but at least as fast especially if you use just the same code as in C. I've examined the assembly generated by visual studio and I can tell you that even C++ class and template code is so well optimized that I won't return to C anymore. If you use a class with non-virtual methods, its the same as a C library with a few functions that get the same struct as their first parameter - just in a more sophisticated and safer way. SWITCH TO C++ NOW IF YOU CAN! Even if you don't use its object oriented features for now - but you should learn object oriented stuff too because its powerful, lets you writing more roboust and bug free code!
pasztorpisti 1-Aug-12 16:28pm    
Ppl program in C only in cases where the architecture doesn't have a better compiler, or when the coder doesn't know c++. I myself misbelieved that c++ is worse/slower than C before actully learning it. :-)
All notable game engines are in C++ today despite the fact that games are one of the most performance critical applications on desktop PCs. C++ code is just as good as C with todays optimizing compilers. I know only about one big difference: exception handling (many says code generated for this counts for around 5% performance loss in average). You can turn this C++ feature off in compilers (as we do).
[no name] 1-Aug-12 17:01pm    
thank you :)
pasztorpisti 1-Aug-12 17:13pm    
You are welcome! Let me recommend not only a free, but actually a very-very good source to learn C++. Its Bruce Eckels "Thinking in C++" volume 1 and 2.
Main Page: http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
A working download mirror: http://www.smart2help.com/e-books/

I learnt java from his Thinking in Java book. "Unfortunately" I was already good in C++ when I found his site. This guy rocks! He knows languages well and knows how to teach. His books teach by example and with very good explanation.
It depends, which one uses the same number of bits as your processor/OS? For example, on a 64-bit system with a 64-bit OS, your best option is probably the data type that is 64-bit. Realistically though, you'll probably get the same performance with anything smaller than that as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900