Difference between using true and TRUE

Started by
8 comments, last by Rydinare 15 years, 11 months ago
I'm starting to learn Directx and I realized when I did all my C++ programming all the functions that were bool used true. Now in a lot of function calls to Directx and WinMain I have things stated as TRUE. What is the difference between the two and why is there the change in Caps (I know that C++ is case sensitive). Also, if you know any good programming websites for DirectX 9 that would be great. Any tips would be also appreciated.
Advertisement
true is a C++ keyword referring to one of two possible values for the bool type. TRUE is a macro declared in windows.h that evaluates to -1, which is useful if you're programming in C (blah blah C99 blah) and don't have a real bool type. Since the Windows API was written for C, not C++, they use that convention.
The difference is in their value types.

"bool" has the same size as a char (i.e. a byte) but accepts the keyword values of "true" and "false".

On the other hand "BOOL" is a int typedef in the Win32 API. It accepts the macro constants "TRUE" and "FALSE" (defined as 1 and 0 respectively). But as an int of course, it can accept ANY int value, in which "0" evaluates as "FALSE" and anything else as "TRUE".

During compilation, my compiler (MSVC++ Express 2008) usually converts between "true" and "TRUE" say.

However, I really would stick to using "true" with bools and "TRUE" with BOOL.
I don't think the C++ standard specifies the size of bool. Google says that older versions of both gcc and MSVC had bools larger than 1.

As other have mentioned bool/true/false are C++ things. BOOL/TRUE/FALSE are Win32 things. Win32 is fundamentally a C API not C++.

In general if a function takes a BOOL it's ok to give it a bool. If it returns a BOOL you can stick it in a bool but you may get a (benign) compiler warning. If a function takes or returns a BOOL* then you have to use a honest BOOL*, failing to do this by casting away the compiler's complaint can corrupt memory.

Since a BOOL is really an int you have to follow the rules of C. e.g. this is potentially invalid code:
BOOL b = Foo();if (b == TRUE)    Whatever();

...because it's possible for b to be (say) 2 and the test fails.
-Mike
Quote:Original post by Sneftel
true is a C++ keyword referring to one of two possible values for the bool type. TRUE is a macro declared in windows.h that evaluates to -1, which is useful if you're programming in C (blah blah C99 blah) and don't have a real bool type. Since the Windows API was written for C, not C++, they use that convention.



I've always seen the windows headers define TRUE to 1, not -1. Not that it matters a whole lot but still. It is more common to see true being 1 though.

As of visual studio 2005, it's defined as 1 in windef.h FYI. :)
Quote:Original post by Anon Mike
I don't think the C++ standard specifies the size of bool. Google says that older versions of both gcc and MSVC had bools larger than 1.

As other have mentioned bool/true/false are C++ things. BOOL/TRUE/FALSE are Win32 things. Win32 is fundamentally a C API not C++.

In general if a function takes a BOOL it's ok to give it a bool. If it returns a BOOL you can stick it in a bool but you may get a (benign) compiler warning. If a function takes or returns a BOOL* then you have to use a honest BOOL*, failing to do this by casting away the compiler's complaint can corrupt memory.

Since a BOOL is really an int you have to follow the rules of C. e.g. this is potentially invalid code:
BOOL b = Foo();if (b == TRUE)    Whatever();

...because it's possible for b to be (say) 2 and the test fails.



I wouldn't worry much about that. If Foo returns BOOL, then unless the programmer has gone completely insane (which happens ;) then Foo will be returning TRUE or FALSE and everything will be fine. A bad programmer would return numbers directly instead of TRUE or FALSE in this case.

Quote:Original post by Anon Mike
I don't think the C++ standard specifies the size of bool.
You're right, it doesn't. All it says is that it's an integral value that has a true and a false state (... and that there are no signed or unsigned versions, but I think that's a bit superfluous to mention).
If a bool has a size of one byte, that's coincidence (or call it an implementation detail), just like int being 4 bytes and short 2 bytes (which is by no means necessarily the case).

It should be noted that the std::vector<bool> template is usually specialized as bit array, so a bool is really only a bit in this case.
Quote:Original post by Anon Mike
I don't think the C++ standard specifies the size of bool.


1 <= bool <= long.
-- gekko
Quote:Original post by SirKnight
If Foo returns BOOL, then unless the programmer has gone completely insane (which happens ;) then Foo will be returning TRUE or FALSE and everything will be fine. A bad programmer would return numbers directly instead of TRUE or FALSE in this case.

Not at all. It's quite common to see C code that does something like:
BOOL AreDifferent(int a, int b){    return a - b;}

Obviously this specific example is overly simplistic and nobody (approximately) would actually do that for two ints, but more complex comparisons can often be boiled down to the moral equivalent. A slightly better example is strcmp. It's because of stuff like this that Win32 BOOL is defined the way it is.

In C any value that is semanticaly boolean, and is not 0, is true. The actual value used for the constant TRUE varies from environment to environment with 1, -1, and (~0) all being in common use.
-Mike
Quote:Original post by Anon Mike
Obviously this specific example is overly simplistic and nobody (approximately) would actually do that for two ints, but more complex comparisons can often be boiled down to the moral equivalent. A slightly better example is strcmp.


Sort of. strcmp() is actually intended to do a three-way comparison (so you can also check which string is "less", for sorting purposes), and its return type isn't documented as trying to be any kind of boolean value. If it were supposed to mean that, don't you think they'd let true mean "equal" and false mean "different" instead of the other way around, as it is? :)

That said, I'm pretty sure I've heard of Win32 API functions that are listed as returning BOOL but actually return some encoded value (and there isn't necessarily even a corresponding enumeration provided). O_O

This topic is closed to new replies.

Advertisement