CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Oct 2002
    Timisoara, Romania

    Visual C++ General: What is the difference between 'BOOL' and 'bool'?

    Q: What is the difference between 'BOOL' and 'bool'?

    A: 'bool' is a built-in C++ type while 'BOOL' is a Microsoft specific type that is defined as an 'int'. You can find it in 'windef.h':

    typedef int                 BOOL;
    #ifndef FALSE
    #define FALSE               0
    #ifndef TRUE
    #define TRUE                1
    The only possible values for a 'bool' are 'true' and 'false', whereas for 'BOOL' you can use any 'int' value, though 'TRUE' and 'FALSE' macros are defined in 'windef.h' header.

    Q: What is the size of 'BOOL' and 'bool'?

    A: If you use the 'sizeof' operator, it will yield 1 for 'bool', though according to the standard the size of' bool' is implementation defined, and 4 for 'BOOL', on 32-bits platform, where 'sizeof(int)' is 4 bytes. If the size of 'int' changes to 8 bytes on 64-bits platforms, 'sizeof(BOOL)' will yield 8 instead.

    Q: Does 'BOOL' has to do with MFC?

    A: 'BOOL' was used by Microsoft long before 'bool' was actually added to the C++ language, but it has nothing to do with MFC. Many Windows API returns a 'BOOL' to indicate success or failure.

    Q: Is it OK if I test the return of a Windows SDK function against 'TRUE'?

    BOOL ret = SomeSDKFunction();
    if(TRUE == ret)
      // do something
    A: Actually no, it is not OK. If you read the documentation for APIs you can see that for most of them:

    Quote Originally Posted by MSDN
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError().
    'TRUE' is defined as 1, but nothing guarantees that the function actually returns 'TRUE' (a value that is nonzero) or some other value. In fact there are functions that indeed return multiple values like 1, 0 or 1.

    Here are several examples:

    BOOL EnableMenuItem(HMENU hMenu, UINT uIDEnableItem, UINT uEnable );
    This function returns 4 possible values.

    Quote Originally Posted by MSDN
    The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is -1.
    BOOL GetMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax );
    Quote Originally Posted by MSDN
    If the function retrieves a message other than WM_QUIT, the return value is nonzero.
    If the function retrieves the WM_QUIT message, the return value is zero.
    If there is an error, the return value is -1.
    This is another example that other values than 0 and 1 can be used as a 'BOOL'.

    So you should code it like:

    BOOL ret = SomeWinAPI();
      // do something
    And for the functions that are know to return values like -1 you also must take that into account. For instance in the case of 'GetMessage()':

    BOOL ret = GetMessage(...);
    if(ret == 0)
      // WM_QUIT
    else if(ret == -1)
      // error
      // success

    Last edited by Andreas Masur; July 24th, 2005 at 03:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.