C/C++中各种类型int、long、double、char表示范围(最大最小值)
#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; return 0; }
/*执行结果分析:
以上结果已经非常明确了,一下补充说明几点:
概念、整型:表示整数、字符和布尔值的算术类型合称为整型(integral type)。
关于带符号与无符号类型:整型 int、stort 和 long 都默觉得带符号型。要获得无符号型则必须制定该类型为unsigned,比方unsigned long。unsigned int类型能够简写为unsigned,也就是说,unsigned后不加其它类型说明符就意味着是unsigned int。
一字节表示八位,即:1byte = 8 bit;
int: 4byte = 32 bit 有符号signed范围:2^31-1 ~ -2^31即:2147483647 ~ -2147483648无符号unsigned范围:2^32-1 ~ 0即:4294967295 ~ 0
long: 4 byte = 32 bit 同int型
double: 8 byte = 64 bit 范围:1.79769e+308 ~ 2.22507e-308
long double: 12 byte = 96 bit 范围: 1.18973e+4932 ~ 3.3621e-4932
float: 4 byte = 32 bit 范围: 3.40282e+038 ~ 1.17549e-038
int、unsigned、long、unsigned long 、double的数量级最大都仅仅能表示为10亿,即它们表示十进制的位数不超过10个,即能够保存全部9位整数。而short仅仅是能表示5位;
另外对于浮点说而言:使用double类型基本上不会有错。在float类型中隐式的精度损失是不能忽视的,二双精度计算的代价相对于单精度能够忽略。其实,在有些机器上,double类型比float类型的计算要快得多。float型仅仅能保证6位有效数字,而double型至少能够保证15位有效数字(小数点后的数位),long double型提供的精度通常没有必要,并且还要承担额外的执行代价。
double是8字节共64位,当中小数位占52位,2-^52=2.2204460492503130808472633361816e-16,量级为10^-16,故可以保证2^-15的全部精度。
在有些机器上,用long类型进行计算所付出的执行时代价远远高于用int类型进行相同计算的代价,所以算则类型前要先了解程序的细节而且比較long类型与int类型的实际执行时性能代价。
欢迎提出宝贵意见,以帮助我改进,不胜感激!!!
——桑海整理
相关文章
- C++ 多继承和虚继承的内存布局
- C++模板之typename和class关键字的区别
- C/C++ epoll内存计算方法,4G内存服务器epoll并发量最大能达到多少?
- [c++菜鸟]《Accelerate C++》习题解答
- [c++菜鸟]《Accelerate C++》读书笔记
- C++开源代码覆盖率工具OpenCppCoverage介绍(Windows)
- 图像最大熵阈值分割的详细原理及基于OpenCV的C++代码实现
- 70 C++ - 文件读写
- C++ 11 中的右值引用
- C++ explict 关键字
- 《C++面向对象高效编程(第2版)》——2.26 UML 中类的关系
- 从汇编看c++中临时对象的析构时机
- 纪念逝去的岁月——C/C++字符串旋转
- 理清gcc、libc、libstdc++的关系(libstdc++是gcc搞的,libc++是llvm搞的,他们都是C++标准库的实现)
- 【华为OD机试真题 java、python、c++、JsNode】最大利润、贪心的商人(100%通过+复盘思路)
- 167、【动态规划】AcWing ——83. 股票的最大利润 (C++版本)
- c++ constructors not allowed a return type错误问题
- 浅谈C++ STL stack 容器
- 国家模式c++