C++ Note 14: C++ Extension to C - New Data Type bool Type

1. C++ adds the bool type to the basic type system of the C language.

2. The values ​​of bool type variables in C++ are only true and false. True represents the true value, which is represented by 1 in the compiler, false represents the non-true value, and 0 in the compiler.

3. The C++ compiler will convert non-0 values ​​to true (1) and 0 values ​​to false (0) when assigning values.

4. In theory, the bool variable occupies one byte.

 

 

 

 

The following program illustrates the above content:

 

#include<iostream>

using namespace std;

 

intmain()

{

bool a = true;

cout<<"bool a          "<<a<<endl; //验证true的值

cout<<"sizeof bool "<<sizeof(bool)<<endl;//Verify the memory size of bool type variables

cout<<"sizeof a "<<sizeof(a)<<endl; //Verify the memory size of bool type variables

 

bool b = false;

cout<<"bool b "<<b<<endl; //Verify the value of false

 

bool c = 0;

cout<<"bool c "<<c<<endl; //Verify the value of the bool variable is 0

 

bool d = 10;

cout<<"bool d "<<d<<endl; //Verify that the value of the bool variable is not 0

 

bool e = -10;

cout<<"bool e "<<e<<endl; //Verify that the value of the bool variable is not 0

 

system("pause");

return 0;

}

 

 

operation result:

bool a          1

sizeof bool     1

sizeof a        1

bool b          0

bool c          0

bool d          1

bool e          1

 

 

Please press any key to continue . . .

 

 

 

 

 

long press to unlock

 

Unlock more exciting insider

 

programming according to the law

WeChat: Lightspeed-Tech

Technology drives life

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325061058&siteId=291194637