c++学习——bool变量测试(四则运算,所占字节大小)
测试项目:
1.bool 变量的 四则运算
2.bool 变量所在内存大小
测试代码:
[cpp]
1. #include <iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. int iSizeCouner = 0;
8. bool bTestBool = false;
9. bool a = 3;
10. bool b = 1;
11. bool c = a + b;
12. bool d = a - b;
13.
14. // test
15. cout << "a = " << a <<endl;
16. cout << "b = " << b<<endl;
17. cout << "c = " << c<<endl;
18. cout << "d = " << d <<endl;
19.
20. iSizeCouner = sizeof(bTestBool);
21.
22. cout << iSizeCouner <<endl;
23. cout << bTestBool <<endl;
24. cout << bTestBool <<endl;
25.
26. bTestBool = true;
27. cout <<boolalpha<<bTestBool <<endl;
28.
29. while(1);
30. }
打印输出:
a = 1
b = 1
c = 1
d = 0
iSizeCouner = 1
bTestBool = 0
结论:
1.在四则运算中始终都是 0,1 参与运算
2.bool 变量 所占字节大小 为1 字节
摘自 DriverMonkey的专栏
补充:软件开发 , C++ ,