1.整型
#include<iostream>
using namespace std;
int main()
{
//整型
//1、短整型(-2^15--2^15-1=-32768--32767)
short num1 = 10;
//2、整型
int num2 = 10;
//3、长整型
long num3 = 10;
//4、长长整型
long long num4 = 10;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num1 << endl;
cout << "num3 = " << num1 << endl;
cout << "num4 = " << num1 << endl;
system("pause");
return 0;
}
2 sizeof关键字
#include<iostream>
using namespace std;
int main()
{
//整型:short(2) int(4) long(4) long long(8)
//可以利用sizeof求出数据类型占用内存大小
//语法:sizeof(数据类型/变量)
short num1 = 10;
cout << "short 占用内存空间为:" << sizeof(short) << endl;
int num2 = 10;
cout << "int 占用内存空间为:" << sizeof(int) << endl;
long num3 = 10;
cout << "long 占用内存空间为:" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用内存空间为:" << sizeof(num4) << endl;
//整型大小比较
//short < int <= long <= long long;
system("pause");
return 0;
}
3.浮点型
#include<iostream>
using namespace std;
int main()
{
//1、单精度 float
//2、双精度 double
//默认情况下:输出一个小数,会显示出6位有效数字
float f1 = 3.1415926f;
cout << "f1= " << f1 << endl;
double d1 = 3.1415926;
cout << "d1= " << d1 << endl;
//统计float和double占用内存空间
cout << "float占用内存空间为:" << sizeof(f1) << endl;//4字节
cout << "double占用内存空间为:" << sizeof(d1) << endl;//8字节
//科学计数法
float f2 = 3e2;
float f3 = 3e-2;
cout << "f2= " << f2 << endl;
cout << "f3= " << f3 << endl;
system("pause");
return 0;
}
4.字符型
#include<iostream>
using namespace std;
int main()
{
//1、字符型变量创建方式
char ch = 'a';
char ch_ = 'b';
char ch1 = 'A';
char ch1_ = 'B';
cout << ch << endl;
//2、字符串变量所占内存大小:1个字节
cout << "char 字符型变量所占内存空间:" << sizeof(ch) << endl;
//3、字符串变量常见错误
//不能用双引号,要用单引号char ch2 = "b";
//创建字符型变量时,单引号内只能有一个字符char ch2='abcdef';
//4、字符串变量对于ASCII编码
//a - 97,b - 98;
//A - 65, B - 66;
cout << ch <<" "<< (int)ch << endl;
cout << ch_ << " " << (int)ch_ << endl;
cout << ch1 << " " << (int)ch1 << endl;
cout << ch1_ << " " << (int)ch1_ << endl;
system("pause");
return 0;
}
5.转义字符
#include<iostream>
using namespace std;
int main()
{
/*
\a 警报
\b 退格(BS),将当前位置移到前一列
\f 换页,将当前位置移到下一页开头
\n 换行,将当前位置移到下一行开头
\r 回车,将当前位置移到本行开头
\t 水平制表,跳到下一个TAB位置
\V 垂直制表
\\ 代表一个反斜线字符"\"
\' 代表一个单引号
\" 代表一个双引号字符
\?代表一个问号
\
*/
//水平制表符\t总共八个字符
cout << "aaa\thelloworld" << endl;
cout << "aaaaa\thelloworld" << endl;
cout << "aaaaaaa\thelloworld" << endl;
system("pause");
return 0;
}
6.字符串型
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、C风格字符串:char变量名[] = "字符串值"
//注意事项 char字符串名要加[]
//注意事项2 等号后面要用双引号包含字符串
char str1[] = "hello world";
cout << str1 << endl;
//2、C++风格字符串
//注意事项包含头文件#include<string>
string str2 = "hello world";
cout << str2 << endl;
system("pause");
return 0;
}
7.bool数据类型
#include<iostream>
using namespace std;
int main()
{
//bool类型只有两个值:
//true--真(本质是1)
//false--假(本质是0)
//bool占用一个字节大小
//1、创建bool数据类型
bool flag = true;
cout << flag << endl;
bool flag2 = false;
cout << flag2 << endl;
//2、查看bool数据类型所占内存空间
cout << sizeof(flag) << endl;
system("pause");
return 0;
}
8.数据的输入
#include<iostream>
using namespace std;
int main()
{
//1、整型
int a = 0;
cout << "请给整型变量a赋值:";
cin >> a;
cout << "整型变量a= " << a << endl;
//2、浮点型
float f = 3.14f;
cout << "请给浮点型变量f赋值:";
cin >> f;
cout << "浮点型变量f= " << f << endl;
system("pause");
return 0;
}