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

常见的数据范围

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

一、总结

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


shrot 
216    (-2^15  到2^15  -1)-32768327675
int4
32    (-2^31 到 2^31  -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++图形化编程】小游戏——打砖块(1)

【C++图形化编程】小游戏——打砖块(1)

0.前言这篇文章我们尝试创建一个打砖块的小游戏。1.游戏框架根据我们前面做的一些游戏的框架,这个小游戏的框架也可以分为下面这样的框架。int main() { startup();&n...

【数论】同余定理与同余方程

定义同余定理是数论中的一个重要概念。它的定义是这样的:给定一个整数m,如果两个整数a和b满足(a-b)能够被m整除,即(a-b)/m 得到一个整数,那么就成整数a和b对模m同余,记作a≡b(mod m...

【数据结构】并查集1

【数据结构】并查集1

1.引入    对于一个集合S={a1, a2, …, an-1, an},我们还可以对集合S进一步划分: S1,S2,…,Sm-1,Sm,我们希望能够快速确定...

【入门篇】>>> DEVC++下载、安装、简单使用

【入门篇】>>> DEVC++下载、安装、简单使用

什么是DEVC++    DEVC++是一款编程工具,是一个Windows环境下的一个适合于初学者使用的轻量级C/C++ 集成开发环境(IDE),它是一款自由软件,遵守G...

求阶乘的方法

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

【算法】扩展欧几里得算法

一、欧几里得算法我们前面学过求最大公约数的算法:欧几里得算法(又叫辗转相除法) ,一般缩写是gcd,在C++中经常写成如下形式:int gcd(int a,int b)...