Re: bool or BOOL in MFC projects

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"David Lowndes" <DavidL@xxxxxxxxxxxxxxx> wrote in message news:kfrgm35tmrvee99ei8pgh9k5uq5vtdd3b0@xxxxxxxxxx

I recall being bitten by code like this:

if ( Api() == TRUE )
{..
}

... which failed under Windows 95 because the API returned a non-zero
value rather than TRUE.

A few thoughts:

Yes. In fact I avoid "==TRUE" like the plague, and by extension "==true" though that should be safer I guess.

But the whole point of the Boolean concept is that you can use

BOOL b;
....
if( b )

Writing if( b==TRUE ) is a tautology in the sense that "b==TRUE" is an expression which (in principle) evaluates to a Boolean, which you are then testing with if() :-)

In fact, correct me if I am wrong, expressions comparing integers a<b a==b return an integer 1 or 0 don't they? My 1991 copy of Stroustrup predates "bool", and so I am never sure about things like this :-) They haven't changed it have they? If not, the BOOL concept appears implicit in the language, even if it is Windows which actually defines the type BOOL.

[I vaguely remember that a==b was not at one stage required to return 1 if true, but maybe that is prehistory.]

Anyway the ability of the if( ... ) to accept or reject the operand on the basis of being zero or non-zero is one of the extremely useful facets of C/C++ and I would never write for a pointer p if( p==NULL ) or the other favourite if( NULL==p ) because if( p ) is perfectly clear!

Just my 2d worth,

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm





.



Relevant Pages

  • Re: Can Windows XP do this? (backup)
    ... BOOL Hibernate, ... BOOL DisableWakeEvent ... Windows NT/2000/XP: Included in Windows 2000 and later. ...
    (microsoft.public.windowsxp.general)
  • Re: A way to bitwise AND two ints
    ... > As defined by Kernighan and Ritchie any non-zero is true. ... boolean type. ... you want positive ints to count as true. ... > int's together and get a bool" was a dead giveaway. ...
    (microsoft.public.dotnet.languages.csharp)
  • How to detect whether terminal services is installed in Windows XP Pro
    ... These code is used to detect whether terminal services is installed in windows. ... However, when I run these code in windowsXP Pro, the result is the terminal services isn't install. ... BOOL AreWeRunningTerminalServices ...
    (microsoft.public.win2000.termserv.apps)
  • Re: Cisco Aironet 350 & CE.NET 4.2
    ... SYSINTR seems ok, I/O seems ok, 4.1 works ok, 4.2 doesn't. ... >> Michel Verhagen, eMVP ... >> Windows CE Consultant ... >> BOOL ...
    (microsoft.public.windowsce.platbuilder)
  • Re: rationale for #define true 1 in stdbool.h
    ... it's desirable because assigning any non-zero value ... I have accidentally written code like this: ... until I looked up the definition of 'bool'. ... be a typedef for unsigned char). ...
    (comp.lang.c)