您的位置:首页 > 编程语言 > C语言/C++

输出C++基本数据类型的数据范围

2018-01-31 19:19 579 查看

1. 机器字长:机器字长是指计算机进行一次整数运算所能处理的二进制数据的位数(整数运算即定点整数运算)。因为计算机中数的表示有定点数和浮点数之分,定点数又有定点整数和定点小数之分,这里所说的整数运算即定点整数运算。机器字长也就是运算器进行定点数运算的字长,通常也是CPU内部数据通道的宽度。。

算术类型的存储空间按照机器而定。一般,short类型为半个机器字长,int为一个机器字长,long为1或2个机器字长,float为一个机器字长,double为两个字,long double用3或4个字长。

2. C++基本数据类型

数据类型名称字节数别名取值范围
int*signed,signed int由操作系统决定,即与操作系统的"字长"有关
unsigned int*unsigned由操作系统决定,即与操作系统的"字长"有关
__int81char,signed char–128 到 127
__int162short,short int,signed short int–32,768 到 32,767
__int324signed,signed int–2,147,483,648 到 2,147,483,647
__int648–9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
bool1false 或 true
char1signed char–128 到 127
unsigned char10 到 255
short2short int,signed short int–32,768 到 32,767
unsigned short2unsigned short int0 到 65,535
long4long int,signed long int–2,147,483,648 到 2,147,483,647
long long8none (but equivalent to __int64)–9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long4unsigned long int0 到 4,294,967,295
enum*由操作系统决定,即与操作系统的"字长"有关
float43.4E +/- 38 (7 digits)
double81.7E +/- 308 (15 digits)
long double81.7E +/- 308 (15 digits)
wchar_t2__wchar_t0 到 65,535

3. 输出C++基本数据类型的数据范围

1. lmits.h

头文件lmits.h定义了基本数据类型的范围,下面代码为limits.h中的部分代码,使用numeric_limits<数据类型>::min()和numeric_limits<数据类型>::max()可以得到对应数据类型的数据范围。

template<typename _Tp>
struct numeric_limits : public __numeric_limits_base
{
static _GLIBCXX_CONSTEXPR _Tp
min() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }

static _GLIBCXX_CONSTEXPR _Tp
max() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }

.......
}


实现代码如下:

#include<iostream>
#include<limits.h>
using namespace std;
int main(){
cout<<"基本数据类型范围\n";

cout<<"char    :"<<numeric_limits<char>::min()<<'-'<<numeric_limits<char>::max()<<'\n';
cout<<"short   :"<<numeric_limits<short>::min()<<'-'<<numeric_limits<short>::max()<<'\n';
cout<<"int     :"<<numeric_limits<int>::min()<<'-'<<numeric_limits<int>::max()<<'\n';
cout<<"unsigned int   :"<<numeric_limits<unsigned int>::min()<<'-'<<numeric_limits<unsigned int>::max()<<'\n';
cout<<"long    :"<<numeric_limits<long>::min()<<'-'<<numeric_limits<long>::max()<<'\n';
cout<<"long long   :"<<numeric_limits<long long>::min()<<'-'<<numeric_limits<long long>::max()<<'\n';
cout<<"unsigned long long   :"<<numeric_limits<unsigned long long>::min()<<'-'<<numeric_limits<unsigned long long>::max()<<'\n';
cout<<"float   :"<<numeric_limits<float>::min()<<'-'<<numeric_limits<float>::max()<<'\n';
cout<<"double  :"<<numeric_limits<double>::min()<<'-'<<numeric_limits<double>::max()<<'\n';
cout<<"long double   :"<<numeric_limits<long double>::min()<<'-'<<numeric_limits<long double>::max()<<'\n';
return 0;
}


或输出宏定义

#include<iostream>
#include<climits>
using namespace std;
int main(){
cout<<"整型数据类型范围\n";
cout<<"char    :"<<CHAR_MIN<<'-'<<CHAR_MAX<<'\n';
cout<<"short   :"<<SHRT_MIN<<'-'<<SHRT_MAX<<'\n';
cout<<"int     :"<<INT_MIN<<'-'<<INT_MAX<<'\n';
cout<<"long    :"<<LONG_MIN<<'-'<<LONG_MAX<<'\n';
cout<<"long long   :"<<LLONG_MIN<<'-'<<LLONG_MAX<<'\n';
return 0;
}


可输出的宏定义如下:

nameexpressesvalue
CHAR_BITNumber of bits in a char object (byte)8 or greater*
SCHAR_MINMinimum value for an object of type signed char-127 (-27+1) or less*
SCHAR_MAXMaximum value for an object of type signed char127 (27-1) or greater*
UCHAR_MAXMaximum value for an object of type unsigned char255 (28-1) or greater*
CHAR_MINMinimum value for an object of type chareither SCHAR_MIN or 0
CHAR_MAXMaximum value for an object of type chareither SCHAR_MAX or UCHAR_MAX
MB_LEN_MAXMaximum number of bytes in a multibyte character, for any locale1 or greater*
SHRT_MINMinimum value for an object of type short int-32767 (-215+1) or less*
SHRT_MAXMaximum value for an object of type short int32767 (215-1) or greater*
USHRT_MAXMaximum value for an object of type unsigned short int65535 (216-1) or greater*
INT_MINMinimum value for an object of type int-32767 (-215+1) or less*
INT_MAXMaximum value for an object of type int32767 (215-1) or greater*
UINT_MAXMaximum value for an object of type unsigned int65535 (216-1) or greater*
LONG_MINMinimum value for an object of type long int-2147483647 (-231+1) or less*
LONG_MAXMaximum value for an object of type long int2147483647 (231-1) or greater*
ULONG_MAXMaximum value for an object of type unsigned long int4294967295 (232-1) or greater*
LLONG_MINMinimum value for an object of type long long int-9223372036854775807 (-263+1) or less*
LLONG_MAXMaximum value for an object of type long long int9223372036854775807 (263-1) or greater*
ULLONG_MAXMaximum value for an object of type unsigned long long int18446744073709551615 (264-1) or greater*
可以看到,基本整形数据类型的最大,最小值都有相应宏定义,不过无符号数的最小值都没有定义,大概是默认为零,不作定义;同样没有定义的还有浮点数类型。

2. climits

limits.h为c语言中的头文件(C++中自然也能使用)

c++对应头文件为climits

如果要输出数据范围,climits和limits.h在使用上是有差别的。

如果用climits文件替代limits.h,numeric_limits<数据类型>::min()和numeric_limits<数据类型>::max()就会使用不了。

如果用使用climits头文件,上述两种方法中只有宏定义能用。

3. float.h和cfloat

float.h/cfloat定义了浮点数数据类型的范围

使用它们作为头文件,即可输出浮点数类型的范围,输出整型数据类型的范围也是可以的:

#include<iostream>
#include<cfloat>
using namespace std;
int main(){
cout<<"浮点数类型数据范围\n";
cout<<"int     :"<<INT_MIN <<'-'<<INT_MAX<<'\n';
cout<<"float   :"<<FLT_MIN <<'-'<<FLT_MAX<<'\n';
cout<<"double  :"<<DBL_MIN<<'-'<<DBL_MAX<<'\n';
cout<<"long double   :"<<LDBL_MIN<<'-'<<LDBL_MAX<<'\n';
return 0;
}


更多浮点数类型可以参考http://www.cplusplus.com/reference/cfloat/

4. 总结(软件为dev C++):

\limits.hclimitsfloat.h和cfloat
使用numeric_limits<数据类型>::(min()ormax())YesNoNo
使用宏定义输出整型数据范围YesYesYes
使用宏定义输出浮点数数据范围NoNoYes
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: