目录
1 整型
2 sizeof关键字
4 字符型
5 转义字符
6 字符串型
7 布尔类型
8 数据的输入
c++规定在创建一个变量或常量是,必须要指定出相应的数据类型,否则无法给变量分配内存。
1 整型
作用:整型变量表示的是整数类型的数据
c++中能够表示整形的类型有以下几种方式,区别在于所占内存空间不同。占用空间不同导致取值范围不同
| 数据类型 | 占用空间 | 取值范围 |
| short (短整型) | 2字节 | (-2^15~2^15-1) |
| int(整型) | 4字节 | (-2^31~2^31-1) |
| long(长整型) | windows(4字节),linux(32位为4字节、64位为8字节) | (-2^31~2^31-1) |
| long long(长长整型) | 8字节 | (-2^63~2^63-1) |
#includeusing namespace std; int main() { //1.short短整型(-32768~32767) short num1 = 32768; //输出-32768,溢出 //2.int整型 int num2 = 10; //3.long长整型 long num3 = 10; //4.long long长整型 long long num4 = 10; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; cout << "num3 = " << num3 << endl; cout << "num4 = " << num4 << endl; system("pause"); return 0; }
2 sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法:sizeof(数据类型/变量)
#includeusing namespace std; int main() { //可以用sizeof求出数据类型占用内存的大小 //语法:sizeof(数据类型/变量) short num1 = 10; int num2 = 10; long num3 = 10; long long num4 = 10; 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; }
3 实型(浮点型)
作用:用于表示小数。
浮点型变量分为两种:
- 单精度float
- 双精度double
两者的区别在于表示的有效数字的范围不同。
| 数据类型 | 占用空间 | 有效数字范围 |
| float | 4字节 | 7位有效数字 |
| double | 8字节 | 15~16位有效数字 |
#includeusing namespace std; int main() { //默认情况下输出一个小鼠,会显示出6位有效数字。 //1.单精度 float f1 = 3.1415926f; //2.双精度 double d1 = 3.1415926; cout << "f1 = " << f1 << endl; cout << "d1 = " << d1 << endl; //统计float和double占用的内存空间 cout << "float所占用的内存空间为 " << sizeof(float) << endl; cout << "double所占用的内存空间为 " << sizeof(double) << endl; //科学计数法 float f2 = 3e2;//3*10^2; cout << "f2=" << f2 << endl; float f3 = 3e-2;//3*10^(-2) cout << "f3=" << f3 << endl; system("pause"); return 0; }
4 字符型
作用:字符型变量用于显示单个字符。
语法:char ch='a';
注意:
- 在显示字符型变量时,用单引号括起来,不要用双引号;
- 单引号内只能有一个字符,不可以是字符串;
- c和c++中字符型变量只占用一个字节;
- 字符型变量并不是把字符本身放到内存中存储,而是将对应得ASCII码放入存储单元。
#includeusing namespace std; int main() { //1.字符型变量创建方式 char ch = 'a'; cout << ch << endl; //2.字符型变量所占内存大小 cout <<"char所占内存空间大小为" << sizeof(char) << endl; //3.字符型变量常见错误 //char ch2 = "b"; //char ch2 = 'asd'; //4.字符型变量对应ASCII编码 cout << (int)ch << endl; system("pause"); return 0; }
5 转义字符
作用:用于表示一些不能显示出来得ASCII字符。
常用得转义字符有n、\、t等等 c++转义字符
#includeusing namespace std; int main() { //转义字符 //换行符号n cout << "hello worldn"; //反斜杠\ cout << "\" << endl; //水平制表t cout << "aaathello world" << endl; cout << "aaaathello world" << endl; cout << "aathello world" << endl; cout << "aaaaaathello world" << endl; system("pause"); return 0; }
6 字符串型
作用:表示一串字符
两种风格:
- C风格字符串:char 变量名[] = “字符串值”
- C++风格字符串:string 变量名 = “字符串值”
#include#include using namespace std; int main() { //C风格字符串 //注意事项:[] "" char str1[] = "hello world"; cout << str1 << endl; //C++风格字符串 //注意事项:包含一个头文件 string str2 = "hello world"; cout << str2 << endl; system("pause"); return 0; }
7 布尔类型
作用:布尔数据类型代表真或假的值
bool类型只有两个值:
- true代表真,本质是1;
- false代表假,本质是0。
bool类型站1个字节大小
#includeusing namespace std; int main() { //创建bool数据类型 //本质 1代表真 0代表假 bool flag = true; cout << flag << endl; flag = false; cout << flag << endl; //查看bool类型所占的内存空间 cout << "size of bool = " << sizeof(bool) << endl; system("pause"); return 0; }
8 数据的输入
作用:用于从键盘获取数据
关键字:cin
语法:cin >> 变量
#include#include using namespace std; int main() { //整型输入 int a = 0; cout << "input a = "; cin >> a; cout << "output a = " << a << endl; //浮点型输入 float f = 3.14f; cout << "input f = "; cin >> f; cout << "output f = " << f << endl; //字符型 char ch = 'a'; cout << "input ch = "; cin >> ch; cout << "output ch = " << ch << endl; //字符串型 string str = "vvkgs"; cout << "input str = "; cin >> str; cout << "output str = " << str << endl; //bool型 bool flag = false; cout << "input flag = "; cin >> flag;//bool类型只要输入是非0的值都代表真 cout << "output flag = " << flag << endl; system("pause"); return 0; }



