1. bool type is a standard type of C ++
We often encounter BOOL. What is the difference between BOOL and bool?
The BOOL type is typically simulated by programmers themselves or by reading library files. The following two methods can be used for simulation:
(1) typedef
typedef int BOOL;#define TRUE 1#define FALSE 0
This BOOL type is actually int type, but it is easy to use and remember.
(2) enum
typedef enum{true,false} BOOL ;
2. Use the bool type to replace BOOL.
(1) The bool type is the standard type of C ++, Which is safer and more reliable to use.
(2) The bool type is generally 1 byte (sizeof (bool), and the two simulated BOOL types are 4 bytes.
3. About the _ Bool type in C Language
(1) C ++ standard: C ++ 98
(2) C Standard: C89, C99
(3) At first, there was no built-in bool type in the C language standard, so when programmers wanted to use the bool type, they could use the above two methods to simulate it themselves.
The bool type is specified in C99. Because the bool type is already used in C ++ 98, _ Bool is used in C99.
(4) VC6.0 does not support C99, so the _ Bool type cannot be used.
4. bool type implementation in C ++
(1) In wtype. h, as defined below:
typedef short VARIANT_BOOL;
typedef VARIANT_BOOL _VARIANT_BOOL;
(2) In objidl. h, it is defined as follows:
#if 0/* bool is a standard type in C++, and a standard macro expanding to the _Bool type in C99 (see stdbool.h) */ _VARIANT_BOOL bool;#endif
This is a bit confusing. Is the bool type short? But what is the difference between bool and short in actual use? (Bool 1 byte, short 2 byte, bool value is only 0 and 1)
Next, I need to find some information and try to understand this.