天天看点

C++从0到1(2):数据类型

目录

    • 1. 整型
    • 2. sizeof关键字
    • 3. 实型(浮点型)
    • 4. 字符型
    • 5. 转义字符
    • 6. 字符串型
    • 7. 布尔类型
    • 8. cin(数据的输入)
  • C++规定在创建一个变量或常量时,必须要指定相应的数据类型,否则无法给变量分配内存。
  • 数据类型存在的意义:给变量分配一个合适的内存空间

1. 整型

  • 作用: 整型变量表示整数类型的数据
  • 类型:
数据类型 占用空间 取值范围
short (短整型) 2字节 − 2 15 -2^{15} −215 ~ 2 15 2^{15} 215-1(-32768–32767)
int (整型) 4字节 − 2 31 -2^{31} −231 ~ 2 31 2^{31} 231-1
long (长整型) Win4字节,Linux:32位4字节,64位8字节 − 2 31 -2^{31} −231 ~ 2 31 2^{31} 231-1
longlong(长长整型) 8字节 − 2 62 -2^{62} −262 ~ 2 62 2^{62} 262-1
#include<iostream>
using namespace std;

int main()
{
	//1、短整型
	short num1 = 32768;//输出-32768
	//2、整型
	int num2 = 32768;
	//3、长整型
	long num3 = 10;
	//4、长长整型
	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(数据类型/变量)
#include<iostream>
using namespace std;

int main()
{
	//语法:sizeof(数据类型/变量)
	short num1 = 10;
	cout << "num1 size:" << sizeof(num1) << endl;
	cout << "short size:" << sizeof(short) << endl;
	int num2 = 2;
	cout << "int size:" << sizeof(int) << endl;
	long num3 = 4;
	cout << "long size:" << sizeof(long) << endl;
	long long num4 = 70;
	cout << "long long size:" << sizeof(long long) << endl;
	system("pause");
	return 0;
}
           
C++从0到1(2):数据类型

3. 实型(浮点型)

  • 作用: 表示小数
  • 类型:

            单精度float:4字节,7位有效数字

            双精度double:8字节,15~16位有效数字

#include<iostream>
using namespace std;

int main()
{
	//单精度:float
	//双精度:double
	//编译器默认小数位双精度,需要加f,
	//否则还要把双精度转换为单精度
	float f1 = 3.1415926f;
	cout << "f1:" << f1 << endl;
	double d1 = 3.1415926;
	cout << "d1:" << d1 << endl;
	//默认情况下,输出一个小数,会显示出6位有效数字

	cout << "float size:" << sizeof(float) << endl;
	cout << "double size:" << sizeof(double) << endl;

	//科学计数法表示小数
	float f2 = 3e2;//3*10^2
	cout << "f2:" << f2 << endl;
	float f3 = 3e-2;//3*0.1^2

	system("pause");
	return 0;
}
           
C++从0到1(2):数据类型

4. 字符型

  • 作用: 字符型变量用于显示单个字符
  • 语法: char ch = ‘a’; 单引号、单引号内只有单个字符

C++和C中字符型变量只占1个字节

字符型变量是将对应的ASCII编码(对应的编号)放入储存单元

eg:a — 97、b — 98

#include<iostream>
using namespace std;

int main()
{
	//1、字符型变量创建方式
	char ch1 = 'a';
	cout << "ch1:" << ch1 << endl;
	//2、字符型变量所占内存空间大小
	cout << "char size:" << sizeof(char) << endl;
	//3、字符型变量常见错误
	//char ch2 = "b";//双引号报错
	//char ch2 = 'abc';//多个字符报错
	//4、字符型变量对应ASCII编码
	//强制转为整型
	cout << int(ch1) << endl;
	//a:97   A:65
	system("pause");
	return 0;
}
           

5. 转义字符

  • 作用: 用于表示一些不能显示出来的ASCII字符
  • 常用: \ \n \t
    C++从0到1(2):数据类型
#include<iostream>
using namespace std;

int main()
{
	//转义字符

	//换行符\n
	cout << "hello word\n" << endl;
	//反斜杠
	cout<<"\\"<<endl;
	//水平制表符\t   可以整齐的输出数据
	cout << "aaa\thello" << endl;//aaa加空格一共8个字符
	cout << "aaaaa\thello" << endl;
	cout << "aaaaaa\thello" << endl;
	system("pause");
	return 0;
}
           
C++从0到1(2):数据类型

6. 字符串型

  • 作用: 表示一串字符
  • 风格:

        C风格字符串:char 名称[] = “字符串”;

        C++风格字符串:string 名称 = “字符串”;

#include<iostream>
using namespace std;
#include<string>//用C++风格写字符串,要包含这个头文件

int main()
{
	//C风格
	//注意:中括号、双引号
	char str1[] = "hello word";
	cout << "str1:"<<str1 << endl;
	//C++风格
	//#include<string>//用C++风格写字符串,要包含这个头文件
	string str2 = "hi excel";
	cout << "str2:" << str2 << endl;
	system("pause");
	return 0;
}
           
C++从0到1(2):数据类型

7. 布尔类型

  • 作用: 代表真或假的值(占1个字节大小)
  • true:1
  • false:0
#include<iostream>
using namespace std;

int main()
{
	//1、创建布尔类型数据
	bool flag = true;//代表1
	cout << flag << endl;
	
	flag = false;//代表0
	cout << flag << endl;
	//2、查看布尔类型所占内存空间
	cout << "bool size:" << sizeof(bool) << endl;

	system("pause");
	return 0;
}
           
C++从0到1(2):数据类型

8. cin(数据的输入)

  • 作用: 键盘输入
#include<iostream>
using namespace std;
#include<string>//用C++风格写字符串,要包含这个头文件

int main()
{
	//1、整型输入
	int a = 1;
	cout <<"give a a value:" << endl;
	cin >> a;
	cout << "a value is:" << a << endl;
	
	//2、浮点型
	float f = 3.14f;
	cout << "give f a value:" << endl;
	cin >> f;
	cout << "f value is:" << f << endl;
	
	//3、字符型
	char ch = 'a';
	cout << "give ch a value:" << endl;
	cin >> ch;
	cout << "ch value is:" << ch << endl;
	
	//4、字符串型
	string str = "abc";
	cout << "give str a value:" << endl;
	cin >> str;
	cout << "str value is:" << str << endl;
	
	//5、布尔型
	bool flag = true;
	cout << "give flag a value:" << endl;
	cin >> flag;
	cout << "flag value is:" << flag << endl;

	system("pause");
	return 0;
}
           
C++从0到1(2):数据类型

继续阅读