?
Solved

BOOL vs bool

Posted on 1997-10-09
3
Medium Priority
?
2,558 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 80 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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