c++ primer学习指导(19)--2.1.2类型转换

类型转换演示视频 https://www.bilibili.com/video/av82226338/

对应的代码:

 1 #include <iostream>
 2 
 3 int main() {
 4     int i = 42;
 5     std::cout << i << std::endl; // prints 42
 6     if (i) // condition will evaluate as true
 7         i = 0;
 8     std::cout << i << std::endl; // prints 0
 9 
10 
11     bool b = 42;            // b is true
12     std::cout << b << std::endl; // prints 1
13 
14     int j = b;              // j has value 1
15     std::cout << j << std::endl; // prints 1
16 
17     double pi = 3.14;       // pi has value 3.14
18     std::cout << pi << std::endl; // prints 3.14
19 
20     j = pi;                 // j has value 3
21     std::cout << j << std::endl; // prints 3
22 
23     unsigned char c = -1;   // assuming 8-bit chars, c has value 255
24     i = c;  // the character with value 255 is an unprintable character
25             // assigns value of c (i.e., 255) to an int
26     std::cout << i << std::endl; // prints 255
27 
28     return 0;
29 }
原文地址:https://www.cnblogs.com/niao-ge/p/12153668.html