第二章 数据类型
2.1整型
short num1 = 10;
int num2 = 10;
int num3 = 10;
int num4 = 10;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
cout << "num3=" << num3 << endl;
cout << "num4=" << num4 << endl;
cout << "short 类型所占内存空间为: " << sizeof(num1) << endl;
cout << "int 类型所占内存空间为: " << sizeof(num2) << endl;
cout << "long 类型所占内存空间为: " << sizeof(num3) << endl;
cout << "long long 类型所占内存空间为: " << sizeof(num4) << endl;
system("pause");
return 0;
2.2 sizeof关键字
cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
cout << "int 类型所占内存空间为: " << sizeof(int) << endl;
cout << "long 类型所占内存空间为: " << sizeof(long) << endl;
cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;
2.3 实型(浮点型)
float PI = 3.1415926f;
cout << "PI= " << PI << endl;
double d1 = 3.1415926;
cout << "d1= " << d1 << endl;
cout << "float占用内存空间为: " << sizeof(float) << endl;
cout << "double占用内存空间为: " << sizeof(double) << endl;
float f2 = 3e2;
cout << "f2= " << f2 << endl;
float f3= 3e-2;
cout << "f3= " << f3 << endl;
2.4 字符型
char ch1 = 'a';
cout << ch1 << endl;
cout << "字符型变量所占内存大小= " << sizeof(ch1) << endl;
cout << (int)ch1 << endl;
char ch3 = 'a';
cout << 'a'-' ' << endl;
cout << char('a'-' ') <<endl;
2.5 转义字符
cout << "HelloWorld" << endl;
cout << "HelloWorld\n";
cout << "\\" << endl;
cout << "aaa\t" << "Hello" << endl;
2.6字符串型
char str1[] = "helloworld";
cout << str1 << endl;
string str2 = "HelloBeijing";
cout << str2 << endl;
2.7 bool数据类型
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
cout << "bool类型所占内存大小:" << sizeof(bool) << "字节" << endl;
cout << "bool类型所占内存大小:" << sizeof(flag) << "字节" << endl;
2.8 数据的输入
int ib = 0;
cout << "请给整型变量ib赋值:" << endl;
cin >> ib;
cout << "整型变量ib= " << ib << endl;
float fb = 3.1415;
cout << "请给浮点型变量fb赋值:" << endl;
cin >> fb;
cout << "浮点变量fb= " << fb << endl;
char cb = 'a';
cout << "请给字符型变量cb赋值:" << endl;
cin >> cb;
cout << "字符变量cb= " << cb << endl;
string sb = "加油!";
cout << "请给字符串型变量sb赋值:" << endl;
cin >> sb;
cout << "字符串变量sb= " << sb << endl;
bool bb = true;
cout << "请给bool类型变量bb赋值:" << endl;
cin >> bb;
cout << "bool类型变量bb= " << bb << endl;