当前位置:首页 > C++知识 > 正文内容

常见的数据范围

亿万年的星光2年前 (2022-10-07)C++知识935

一、总结

名称字节位数(二进制)最小值最大值位数(十进制)
bool
1
801
1
char18


shrot 
216    (2^16  -1)-32768327675
int4
32    (2^32  -1)-21474836482147483647 10
unsigned int 4320429496729510
long
432-2147483648214748364710
long long 864
-9223372036854775808922337203685477580719
float
4321.17549e-0383.40282e+038
double8
642.22507e-3081.79769e+308
string864


二、测试代码

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


扫描二维码推送至手机访问。

版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。

分享给朋友:

相关文章

C++整型的数据范围

数据类型标识符占字节数数值范围数值范围短整型short [int]2(16位)-32768~32767-2^15 到2^15  -1整型[long] int4(32位)-...

求阶乘的方法

1.普通求法#include<iostream> using namespace std; int main(){ int sum=1;...

【数据结构】栈—表达式括号匹配

【数据结构】栈—表达式括号匹配

【题目描述】假设一个表达式有英文字母(小写)、运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符。请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”;否则...

【数论】杨辉三角

【数论】杨辉三角

一、起源 杨辉三角,是二项式系数在三角形中的一种几何排列。在欧洲,这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年,比贾宪迟600年。杨辉三角...

【数论】常见的距离度量方法

【数论】常见的距离度量方法

一、欧式距离欧式距离(Eucliden Metric,也是欧几里得度量)是一个通常采用的距离定义,旨在m维空间中两个点之间的真实距离,或者向量的自然长度(即该点到原点距离)。在二维和三维空间中的欧氏距...

如何计算一个程序的运行时间(防止超时)

再一些OJ系统中,做题的时候常常会超时,但是很多人不知道自己的程序是否会超时,不知道如何检查自己的程序。这篇文章主要介绍几种监测自己程序运行时间的程序。头文件<time.h> ...