c++的数据类型

1. 整型

#include <iostream>
using namespace std;
int main01()
{
	//1、短整型  -32768 ~ 32767
	short num1 = 10;
	//2.整型
	int num2 = 10;
	long num3 = 10;
	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 main02()
{
	//整型:short(2)  int(4)  long(4) long long(8)
	//可以用sizeof求出数据类型占用内存大小
	//语法:sizeof(数据类型 /变量)

	short num1 = 10;
	//2.整型
	int num2 = 10;
	long num3 = 10;
	long long num4 = 10;

	cout << "short占用内存空间为:" << sizeof(short)<< endl;
	cout << "int占用内存空间为:" << sizeof(num2) << endl;
	cout << "num3 = " << num3 << endl;
	cout << "num4 = " << num4 << endl;
	system("pause");
	return 0;
}

3. 实型

#include <iostream>
using namespace std;
int main03()
{
	//1、单精度 float
	//2、双精度 double
	//默认下,输出一个小数,会显示6位有效数字
	float f1 = 3.14f;//加f告诉它是单精度,否则默认为双精度,还需要转换浪费资源
	double f2 = 3.14;
	float f3 = 3.1415926f;
	double f4 = 3.1415926;
	cout << "f1 = " << f1 << endl;
	cout << "f2 = " << f2 << endl;
	cout << "f3 = " << f3 << endl;
	cout << "f4 = " << f4 << endl;
	
	//
	cout << "float占用内存空间为:" << sizeof(float) << endl;
	cout << "double占用内存空间为:" << sizeof(double) << endl;

	//科学计数法
	float d1 = 3e2;//3*10^2
	cout << "d1 = " << d1 << endl;
	float d2 = 3e-2;//3*0.1^2
	cout << "d2 = " << d2 << endl;
	system("pause");
	return 0;
}

4. 字符型

#include <iostream>
using namespace std;
int main04()
{
	//1.字符型变量创建方式
	char ch = 'a';
	cout << ch << endl;
	
	//2.字符型变量所占内存大小
	cout << "char字符型变量所占内存:" << sizeof(char) << endl;

	//3.字符型变量常见错误
	//char ch2 = "b";只能用单引号
	//char ch3='abcdef;'单引号只能有一个字符

	//4.字符型变量对应ASCII编码
	//a-97  A-65
	cout << (int)ch << endl;
	system("pause");
	return 0;
}

5. 转义字符

#include <iostream>
using namespace std;
int main05()
{
	//转义字符    
    //换行 \n
	cout << "hello world\n";
	
	//反斜杠 \\
	cout<<"\\"<<endl;
	//水平制表符 \t  作用整齐输出字符 \t占8个字符
	cout << "aaa\thelloworld" << endl;
	cout << "aa\thelloworld" << endl;
	cout << "aaaa\thelloworld" << endl;
	cout << "aaa helloworld" << endl;
	cout << "aa helloworld" << endl;
	cout << "aaaa helloworld" << endl;
	system("pause");
	return 0;

}

6. 字符串型

#include <iostream>
using namespace std;
#include <string>//用c++风格字符串的头文件
int main06()
{
	//1.c风格字符串
	//注意事项  char 字符串名 [] = "字符串";
	char str[] = "hello world";
	cout << str << endl;
	//2.c++风格字符串
	//注意事项  string 变量名 = "字符串"
	//          #include <string>
	string str2 = "hello c++";
	cout << str2 << endl;
	system("pause");
	return 0;
}

7. 布尔类型

#include <iostream>
using namespace std;
#include <string>
int main07()
{
	//1.创建bool数据类型
	bool flag = true;
	cout << flag << endl;
	//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型
	bool flag = false;
	cout << "请给布尔类型flag赋值:"<< endl;
	cin >>flag;//非0的值都代表真
	cout << "布尔类型变量flag=" << flag << endl;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值