C++——数据类型

1、数据类型-整型

#include <iostream>
using namespace std;

int main()
{
	//整型
	//1、短整型(-32768 ~ 32767)
	short num1 = 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关键字

#include <iostream>
using namespace std;

int main()
{
	//整型:short(2)int(4)long(4)long long(8)
	//可以利用sizeof求出数据类型占用内存大小
	//语法:sizeof(数据类型/变量)

	short num1 = 10;
	cout << "short占用内存空间为:" << sizeof(num1) << endl;

	int num2 = 10;
	cout << "int占用内存空间为:" << sizeof(int) << endl;

	long num3 = 10;
	cout << "long占用内存空间为:" << sizeof(long) << endl;

	long long num4 = 10;
	cout << "long long占用内存空间为:" << sizeof(long long) << 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(float) << endl;	//4字节

	cout << "double占用内存空间为:" << sizeof(double) << endl;	//8字节

	//科学计数法
	float f2 = 3e2;	//3 * 10 ^ 2
	cout << "f2 = " << f2 << endl;

	float f3 = 3e-2; //3 * 0.1 ^ 2
	cout << "f3 = " << f3 << endl;

	system("pause");

	return 0;
}

4、数据类型-字符型

#include <iostream>
using namespace std;

int main()
{
	//1、字符型变量创建方式
	char ch = 'A ';
	cout << ch << endl;

	//2、字符型变量所占内存大小
	cout << "char字符型变量所占内存:" << sizeof(char) << endl;

	//3、字符型变量常见错误
	//char ch2 = "b";	//创建字符型变量时候,要用单引号
	//char ch2 = 'abcdef';	//创建字符型变量时候,单引号内只能有一个字符

	//4、字符型变量对应ASCII编码
	//a - 97
	//A - 65
	cout << (int)ch << endl;

	system("pause");

	return 0;
}

5、数据类型-转义字符

#include <iostream>
using namespace std;

int main()
{
	//转移字符

	//换行符 \n

	cout << "hello c++\n";

	//反斜杠 \\

	cout << "\\" <<endl;

	//水平制表符 \t		作用可以整齐输出数据

	cout << "aaa\thelloc++" << endl;
	cout << "aa\thelloc++" << endl;
	cout << "aaaaa\thelloc++" << endl;

	system("pause");

	return 0;
}

6、数据类型-字符串类型

#include <iostream>
using namespace std;
#include <string>

int main()
{
	//1、C风格字符串
	//注意事项	char 字符串名 []
	//注意事项2	等号后面 要用双引号 包含起来字符串
	char str[] = "hello world";
	cout << str << endl;

	//2、C++风格字符串
	// 包含一个头文件	#include <string>
	string str2 = "hello world";
	cout << str << endl;

	system("pause");

	return 0;
}

7、数据类型-布尔类型

#include <iostream>
using namespace std;

int main()
{
	//1、创建bool数据类型
	bool flag = true;	//true代表真
	cout << flag << endl;

	flag = false;	//false代表假
	cout << flag << endl;

	//本质上 1代表真	0代表假

	//2、查看bool类型所占内存空间

	cout << "bool类型所占内存空间:" << sizeof(bool) << endl;

	system("pause");

	return 0;
}

8、数据类型-数据的输入

#include <iostream>
using namespace std;
#include <string>

int main()
{
	//1、整型
	//int a = 0;
	//cout << "请给整型变量a赋值:" << endl;
	//cin >> a;
	//cout << "整型变量a = " << a << endl;

	//2、浮点型
	//float f = 3.14f;
	//cout << "请给浮点型变量f赋值:" << endl;
	//cin >> f;
	//cout << "浮点型变量f = " << f << endl;

	//3、字符型
	//char ch = 'a';
	//cout << "请给字符型变量ch赋值:" << endl;
	//cin >> ch;
	//cout << "字符型变量ch = " << ch << endl;

	//4、字符串型
	//string str = "hello";
	//cout << "请给字符串 str赋值:" << endl;
	//cin >> str;
	//cout << "字符串str = " << str << endl;
	
	//5、布尔类型
	bool flag = false;
	cout << "请给布尔类型 flag赋值:" << endl;
	cin >> flag;	//bool类型 只要是非0的值都代表真
	cout << "布尔类型flag = " << flag << endl;

	system("pause");

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值