Solved

BOOL vs bool

Posted on 1997-10-09
3
2,549 Views
Last Modified: 2013-11-20
what is the difference between BOOL (TRUE, FALSE)
and bool (true, false). which to use ? when ? can it be mixed ?
0
Comment
Question by:has
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 20 total points
ID: 1307871
The major difference between BOOL and bool is that one is a typedef of int (BOOL) and the other is a C++ type (bool).

You should be very careful if you mix them because the inherent sizes are different: sizeof(BOOL) == sizeof(int) == 4 and sizeof(bool) == 1. That means that if you save a BOOL into a binary stream, you may corrupt the stream if you try to read it back as a bool.

The BOOL is available as long as you include Windows.h. However, bool is available only on compilers that support the new C++ type. If you want to be as portable as possible, you should use BOOL (because there are still C++ compilers that do not support bool).

There are lots of minor problems with mixing BOOL and bool. But if you are consistent, there's no problem.
0
 

Author Comment

by:has
ID: 1307872
thanks
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1307873
Also note - many MFC functions (and Win32 API functions) expect and return BOOL rather than bool.  If you are writing message handlers that return BOOL, say, don't be tempted to change them to return bool instead.  If you do then your code will break.

However, in your own code, using bool has the advantage that the compiler knows about the type, and will stop you (without a cast) assigning arbitary int values to a bool variable (like you can do with BOOL).  It also ensures the a bool only ever contains 'true' or 'false' (0 or 1).  A BOOL with any non-zero value will test as true, but if you do a comparison with TRUE, you get a fail.

So for compiler error detection, and memory size, go for bool, but always convert to/from BOOL when dealing with MFC and Win32 API.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question