C/C++中各种类型int、long、double、char表示范围(最大最小值)

2024-03-25 00:58

本文主要是介绍C/C++中各种类型int、long、double、char表示范围(最大最小值),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址:http://blog.csdn.net/xuexiacm/article/details/8122267

这一内容虽然十分基础,但是有时候也比较容易忽略数值范围的问题!

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<string>  
  3. #include <limits>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     cout << "type: \t\t" << "************size**************"<< endl;  
  9.     cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);  
  10.     cout << "\t最大值:" << (numeric_limits<bool>::max)();  
  11.     cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;  
  12.     cout << "char: \t\t" << "所占字节数:" << sizeof(char);  
  13.     cout << "\t最大值:" << (numeric_limits<char>::max)();  
  14.     cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;  
  15.     cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);  
  16.     cout << "\t最大值:" << (numeric_limits<signed char>::max)();  
  17.     cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;  
  18.     cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);  
  19.     cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();  
  20.     cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;  
  21.     cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);  
  22.     cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();  
  23.     cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;  
  24.     cout << "short: \t\t" << "所占字节数:" << sizeof(short);  
  25.     cout << "\t最大值:" << (numeric_limits<short>::max)();  
  26.     cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;  
  27.     cout << "int: \t\t" << "所占字节数:" << sizeof(int);  
  28.     cout << "\t最大值:" << (numeric_limits<int>::max)();  
  29.     cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;  
  30.     cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);  
  31.     cout << "\t最大值:" << (numeric_limits<unsigned>::max)();  
  32.     cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;  
  33.     cout << "long: \t\t" << "所占字节数:" << sizeof(long);  
  34.     cout << "\t最大值:" << (numeric_limits<long>::max)();  
  35.     cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;  
  36.     cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);  
  37.     cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();  
  38.     cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;  
  39.     cout << "double: \t" << "所占字节数:" << sizeof(double);  
  40.     cout << "\t最大值:" << (numeric_limits<double>::max)();  
  41.     cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;  
  42.     cout << "long double: \t" << "所占字节数:" << sizeof(long double);  
  43.     cout << "\t最大值:" << (numeric_limits<long double>::max)();  
  44.     cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;  
  45.     cout << "float: \t\t" << "所占字节数:" << sizeof(float);  
  46.     cout << "\t最大值:" << (numeric_limits<float>::max)();  
  47.     cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;  
  48.     cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);  
  49.     cout << "\t最大值:" << (numeric_limits<size_t>::max)();  
  50.     cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;  
  51.     cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;  
  52.     // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
  53.     cout << "type: \t\t" << "************size**************"<< endl;  
  54.     return 0;  
  55. }  



/*运行结果分析:

以上结果已经很明白了,一下补充说明几点:

概念、整型:表示整数、字符和布尔值的算术类型合称为整型(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++中各种类型int、long、double、char表示范围(最大最小值)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/843375

相关文章

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决