Understanding and doubts about the bool type in C ++

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.