C++数据类型/数据输入

1.整型

#include <iostream>
using namespace std;
/*
语法:数据类型 变量=变量初始值;
int a=10;
数据类型存在意义:给变量分配合适的内存空间
*/
int main()
{
	//整型
	//1.短整型(-2^15 ~ 2^15-1 即是 -32768 ~ 32767 -1) 占用2字节
	short num1 = 32768;
	//2.整型(-2^31 ~ 2^31-1) 最常用!!!占用4字节
	int num2 = 10; 
	//3.长整型(-2^31 ~ 2^31-1) 占用4字节
	long num3 = 10;
	//4.长长整型(-2^63 ~ 2^63-1) 占用8字节
	long long num4 = 10;
	
	cout << "短整型num1=" << num1 << endl;
	system("pause"); //固定语句
	return 0;//固定语句
}

11
2.sizeof关键字

#include <iostream>
using namespace std;
/*
	sizeof统计数据类型所占内存大小
*/
int main()
{
	short num1 = 10;
	int num2 = 10;
	//第一种表示
	cout << "short占用的内存空间:" << sizeof(num1) << endl;
	cout << "int占用的内存空间:" << sizeof(num2) << endl;
	//第二种表示
	cout << "short占用的内存空间:" << sizeof(short) << endl;
	cout << "int占用的内存空间:" << sizeof(int) << endl;
	system("pause"); //固定语句
	return 0;//固定语句
}

在这里插入图片描述
3.实型

#include <iostream>
using namespace std;

int main()
{
	//1.单精度 float  2.双精度 double
	//默认情况下,输出一个小数,显示6位有效数字
	float f1 = 3.14f;
	cout << "f1=" << f1 << endl;

	double d1 = 3.14;  //默认类型
	cout << "d1=" << d1 << endl;

	//统计内存空间
	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()
{
	//字符型变量创建方式
	char ch = 'a';
	cout << ch << endl;

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

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

	//字符型变量对应的ASCII编码
	//a - 97; A - 65 按照此可以推算其他字母的ASCII编码
	cout << "字符型变量a对应的ASCII编码:" << (int)ch << endl;
	char ch1 = 'A';
	cout << "字符型变量A对应的ASCII编码:" << (int)ch1 << endl;

	system("pause"); //固定语句
	return 0;//固定语句
}

在这里插入图片描述
5.转义字符

#include <iostream>
using namespace std;

int main()
{
	//转义字符

	//换行符\n
	cout << "hello world\n";

	//反斜杠\\输出一个\

	cout << "\\"<<endl;

	//水平制表符\t 使得数据整齐输出
	cout << "aaaa\thelloworld" << endl;
	cout << "aa\thelloworld" << endl;
	cout << "aaaaaa\thelloworld" << endl;

	system("pause"); //固定语句
	return 0;//固定语句
}

在这里插入图片描述
6.字符串型

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

int main()
{
	//1.C语言风格字符串
	//注意事项 char 字符串名需要加[] 
	//注意事项2 等号后面 要用双引号
	char str[] = "hello world";
	cout << str << endl;

	//2.C++风格字符串
	//注意事项 需要在添加头文件#include <string>
	string str2 = "hello world";
	cout << str2 << endl;

	system("pause"); //固定语句
	return 0;//固定语句
}

7.布尔类型

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

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

	flag = false; 
	cout << flag << endl;//输出为0代表假

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

	system("pause"); //固定语句
	return 0;//固定语句
}

在这里插入图片描述
8.数据的输入
作用:用于从键盘获取数据
关键字:cin
语法:cin>>变量

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

int main()
{
	//cin:从键盘输入数据
	//1.整型
	int a = 0;
	cout << "请给整型变量a赋值:" << endl;
	cin >> a;
	cout << "整型变量:" << a << endl;

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

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

	//4.字符串型
	string str = "aanfndg";
	cout << "请给字符串型变量str赋值:" << endl;
	cin >> str;
	cout << "字符串变量:" << str << endl;

	//5.bool类型 非0值均代表真
	bool flag = true;
	cout << "请给boll型变量flag赋值:" << endl;
	cin >> flag;
	cout << "bool变量:" << flag << endl;

	system("pause"); //固定语句
	return 0;//固定语句
}

本笔记为学习记录

学习资源:https://www.bilibili.com/video/BV1et411b73Z?p=16

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值