C/C++里一些常用的变量类型的范围及相关解释

C/C++里一些常用的变量类型的范围及相关解释
1、关于char的取值范围
(1)char占一个字节,每个字节有8位。char又分为有符号型(signed char)和无符号型(unsigned char)。
这二者的不同之处就在于有符号型将最高位作为了符号位,0代表正数,1代表负数。
无符号型的取值范围为0-255(2^8-1),这个很容易理解,因为char型只有8位。
有符号型的取值范围位-128(-27-1)-127(27-1)
出现这种情况是因为计算机是以补码的形式来存储数值的,
而补码有一条规则:正数的补码是其本身,负数的补码是其取反(取反时符号位不变)加一得到。
若说负0与正0则是针对原名来说的:
127:0111,1111 (反码):1111,1111 (补码):1111,1111
0:0000,0000 (反码):0000,0000 (补码):0000,0000
-1:1000,0001 (反码):1111,1110 (补码):1111,1111
-2:1000,0010 (反码):1111,1101 (补码):1111,1110

-127:1111,1111 (反码):1000,0000 (补码):1000,0001
-128:1000,0000 (反码):1111,1111 (补码):1000,0000
当我们对127+1的时候,所得到的结果就是-128.在我看来,主要是对于符号位的理解。当自己整理一遍后,就会对其有了深刻的认识。

(2)unsigned char范围是0~255
执行下面这个循环的时候会发现其实他是一个死循环
for(int k = 0; k <=255; k ++)
{
unsigned char i=k;
//继续其它。
}
K=255时,再加一则会变为0
255:1111,1111
0:(1)0000,0000
因为unsigned char类型只有8位,多余的一位被溢出了。

2、浮点类型的单精度值具有 4 个字节,即32位,包括一个符号位、一个 8 位 excess-127 二进制指数和一个 23 位尾数。尾数表示一个介于 1.0 和 2.0 之间的数。由于尾数的高顺序位始终为 1,因此它不是以数字形式存储的。此表示形式为 float 类型提供了一个大约在 -3.4E+38 和 3.4E+38 之间的范围。

注意:由于指数是以无符号形式存储的,因此指数的偏差为其可能值的一半。对于 float 类型,偏差为 127;对于 double 类型,偏差为 1023。您可以通过将指数值减去偏差值来计算实际指数值。
所以float的指数范围位float的指数范围为-127~128
float的范围为-2^127 ~ +2^128,也即-1.17E+38 ~ +3.40E+38

下面的程序可以测试一部分变量类型的大小:


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

int main()
{
	cout << "type: \t\t" << "************size**************" << endl;
	cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
	cout << "\t最大值:" << (numeric_limits<bool>::max)();
	cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
	cout << "char: \t\t" << "所占字节数:" << sizeof(char);
	cout << "\t最大值:" << (numeric_limits<char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
	cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
	cout << "\t最大值:" << (numeric_limits<signed char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
	cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
	cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
	cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
	cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
	cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
	cout << "short: \t\t" << "所占字节数:" << sizeof(short);
	cout << "\t最大值:" << (numeric_limits<short>::max)();
	cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
	cout << "int: \t\t" << "所占字节数:" << sizeof(int);
	cout << "\t最大值:" << (numeric_limits<int>::max)();
	cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
	cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
	cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
	cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
	cout << "long: \t\t" << "所占字节数:" << sizeof(long);
	cout << "\t最大值:" << (numeric_limits<long>::max)();
	cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
	cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
	cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
	cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
	cout << "double: \t" << "所占字节数:" << sizeof(double);
	cout << "\t最大值:" << (numeric_limits<double>::max)();
	cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
	cout << "long double: \t" << "所占字节数:" << sizeof(long double);
	cout << "\t最大值:" << (numeric_limits<long double>::max)();
	cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
	cout << "float: \t\t" << "所占字节数:" << sizeof(float);
	cout << "\t最大值:" << (numeric_limits<float>::max)();
	cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
	cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
	cout << "\t最大值:" << (numeric_limits<size_t>::max)();
	cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
	cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
	// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
	cout << "type: \t\t" << "************size**************" << endl;
	system("pause");
	return 0;
}

程序摘自:
http://www.cnblogs.com/tenlee/p/4420102.html
文章借鉴:
https://blog.****.net/wsccdsn/article/details/8180301
https://baike.sogou.com/v154979876.htm?fromTitle=FLOAT