c++学习笔记04--关键字
1. sizeof
作用:利用sizeof可以统计数据类型所占内存大小。
语法:sizeof(数据类型/变量)。
示例:
#include <iostream>
using namespace std;
int main() {
int a = 10;
short b = 10;
long c = 10;
long long d = 10;
cout << "sizeof a=" << sizeof(a) << endl;
cout << "sizeof b=" << sizeof(b) << endl;
cout << "sizeof c=" << sizeof(c) << endl;
cout << "sizeof d=" << sizeof(d) << endl;
system("pause");
return 0;
}
2. cin
作用:用于从键盘获取数据。
语法:cin >> 变量
#include <iostream>
#include <string>
using namespace std;
int main() {
//整形
int a = 0;
cout << "请给整形a赋值" << endl;
cin >> a;
cout << "a = " << a << endl;
//浮点型
float b = 3.14F;
cout << "请给浮点型b赋值" << endl;
cin >> b;
cout << "b = " << b << endl;
//字符型
char c = 'a';
cout << "请给字符型c赋值" << endl;
cin >> c;
cout << "c = " << c << endl;
//字符串型
string d = "hello";
cout << "请给字符串型d赋值" << endl;
cin >> d;
cout << "d = " << d << endl;
//布尔型
bool e = true;
cout << "请给布尔型e赋值" << endl;
cin >> e;//bool只要非0都为真
cout << "e = " << e << endl;
return 0;
}