整形、实型、字符型、关键词(C++)

文章介绍了C++中的几种基本数据类型,包括整形(如short,int,long,longlong)的内存占用和数值范围,实型(float和double)的精度差异以及科学计数法的使用,还有字符型(char)的内存大小及ASCII值的获取。通过sizeof运算符展示了不同数据类型的内存大小。
摘要由CSDN通过智能技术生成

整形、实型、字符型、关键词

数据类型—整形

数据类型—sizeof关键词

数据类型—实型

数据类型—字符型

#include <iostream>
using namespace std;

int main()
{
	short b = 10;
	cout << "a=" << b << endl;   
	long int c = 10;
	cout << "a=" << c << endl;
	long long int d = 10;
	cout << "a=" << d << endl;
	int a = 10;
	cout << "a=" << a<<endl;
	return 0;
}

今天学了不同数据类型,所占内存大小不一样;尽量写数据类型差不多的;
short 短整型 2        范围为-215次方~215次方-1  
int 整形  4       -231次方~231次方-1 
long  长整形  4          -231次方~231次方-1
long long  长长整形 8     -263次方~263次方-1
 因此对于上面求a的值 就把a的类型定为int;C++中整形定为int7/6sizeof(数据类型/变量)  求数据类型所占内存大小

#include <iostream>
using namespace std;

#include <stdio.h>
int main()
{
	int num1 = 10;
	cout << "num1=" << sizeof(int) << endl;
	short num2 = 10;
	cout << "num1=" << sizeof(short) << endl;
	long num3 = 10;
	cout << "num1=" << sizeof(num3) << endl;
	long long num4 = 10;
	cout << "num1=" << sizeof(num4) << endl;

	system("pause");
	return 0;
}
总结:sizeof(数据类型/变量名)


7/6  本节课学习了数据类型--实型

#include <iostream>
using namespace std;
int main()
{
	单精度   float    4个字节    有七位有效数字
	双精度   double   8个字节    有15-16位效数字
	两者的区别为双精度比单精度更精准;
	科学计数法
	float c = 3e2;   //表示3*10^2;
	cout << "c=" << c << endl; 
	double d = 3e2;   
	cout << "d=" << d << endl;
	float e = 3e-2;
	cout << "e=" << e << endl;
	double f = 3e-2;  //3*0.1^2;
	cout << "f=" << f << endl;
	float a = 3.1415926f;  //注:要是使用float类型,需要在小数后面加上f;
	cout << "a=" << a << endl;
	double b = 3.1415926;  
	cout << "b=" <<  b<< endl;
	system("pause");
	return 0;
}

7/6 本节课学习了char 类型

#include <iostream>
using namespace std;
int main()
{
	//关于 char 要明白四个问题
	//char所占内存大小
	char ch = 'a';
	cout << "ch=" << ch << endl;
	cout << "ch所占的内存大小为:" << sizeof(ch) << endl;
	//char的使用     char ch = 'b';
	//char常见的错误      char ch = 'b'; // 使用char是必须是单引号‘’;单引号‘’里面必须是一个字母;
	//char所对应的ASCLL值     char ch = 'a';
	cout << "ch=" << (int)ch << endl;  //求字母所对应的ASCLL值时需要,强制类型转换,就是在变量前加 int ;
	system("pause");

	return 0;
}

以上内容比较乱,哈哈哈,别在意😂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值