C + + Note 14: C + + Extensions--new data type bool type

Source: Internet
Author: User

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

2, the bool type variable in C + + is only true and false,true represents the truth value, the compiler internally with 1, false represents a non-truth, the compiler internally with 0.

3. The C + + compiler converts a non-0 value to True (1) and a value of 0 to False (0) when the value is assigned.

4. Theoretically, the bool variable takes up one byte.

The following program describes the above:

#include <iostream>

using namespace Std;

int main ()

{

bool A = true;

cout<< "bool A" <<a<<endl; Value to verify True

cout<< "sizeof bool" <<sizeof (BOOL) <<endl;//Verify that the BOOL type variable accounts for the memory size

cout<< "sizeof a" <<sizeof (a) <<endl; Verify that the BOOL type variable accounts for memory size

BOOL B = false;

cout<< "bool B" <<b<<endl; Value to verify False

BOOL C = 0;

cout<< "bool C" <<c<<endl; Verify that the value of the BOOL variable value is 0

bool d = 10;

cout<< "BOOL D" <<d<<endl; Verify that the bool variable value is a value other than 0

BOOL E =-10;

cout<< "bool E" <<e<<endl; Verify that the bool variable value is a value other than 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 Insider Stories

Legal programming

: Lightspeed-tech

Technology-driven Life

C + + Note 14: C + + Extensions--new data type bool type

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.