1. 机器字长:机器字长是指计算机进行一次整数运算所能处理的二进制数据的位数(整数运算即定点整数运算)。因为计算机中数的表示有定点数和浮点数之分,定点数又有定点整数和定点小数之分,这里所说的整数运算即定点整数运算。机器字长也就是运算器进行定点数运算的字长,通常也是CPU内部数据通道的宽度。。
算术类型的存储空间按照机器而定。一般,short类型为半个机器字长,int为一个机器字长,long为1或2个机器字长,float为一个机器字长,double为两个字,long double用3或4个字长。
2. C++基本数据类型
数据类型名称 | 字节数 | 别名 | 取值范围 |
---|---|---|---|
int | * | signed,signed int | 由操作系统决定,即与操作系统的"字长"有关 |
unsigned int | * | unsigned | 由操作系统决定,即与操作系统的"字长"有关 |
__int8 | 1 | char,signed char | –128 到 127 |
__int16 | 2 | short,short int,signed short int | –32,768 到 32,767 |
__int32 | 4 | signed,signed int | –2,147,483,648 到 2,147,483,647 |
__int64 | 8 | 无 | –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
bool | 1 | 无 | false 或 true |
char | 1 | signed char | –128 到 127 |
unsigned char | 1 | 无 | 0 到 255 |
short | 2 | short int,signed short int | –32,768 到 32,767 |
unsigned short | 2 | unsigned short int | 0 到 65,535 |
long | 4 | long int,signed long int | –2,147,483,648 到 2,147,483,647 |
long long | 8 | none (but equivalent to __int64) | –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
unsigned long | 4 | unsigned long int | 0 到 4,294,967,295 |
enum | * | 无 | 由操作系统决定,即与操作系统的"字长"有关 |
float | 4 | 无 | 3.4E +/- 38 (7 digits) |
double | 8 | 无 | 1.7E +/- 308 (15 digits) |
long double | 8 | 无 | 1.7E +/- 308 (15 digits) |
wchar_t | 2 | __wchar_t | 0 到 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;
}
可输出的宏定义如下:
name | expresses | value |
---|---|---|
CHAR_BIT | Number of bits in a char object (byte) | 8 or greater* |
SCHAR_MIN | Minimum value for an object of type signed char | -127 (-27+1) or less* |
SCHAR_MAX | Maximum value for an object of type signed char | 127 (27-1) or greater* |
UCHAR_MAX | Maximum value for an object of type unsigned char | 255 (28-1) or greater* |
CHAR_MIN | Minimum value for an object of type char | either SCHAR_MIN or 0 |
CHAR_MAX | Maximum value for an object of type char | either SCHAR_MAX or UCHAR_MAX |
MB_LEN_MAX | Maximum number of bytes in a multibyte character, for any locale | 1 or greater* |
SHRT_MIN | Minimum value for an object of type short int | -32767 (-215+1) or less* |
SHRT_MAX | Maximum value for an object of type short int | 32767 (215-1) or greater* |
USHRT_MAX | Maximum value for an object of type unsigned short int | 65535 (216-1) or greater* |
INT_MIN | Minimum value for an object of type int | -32767 (-215+1) or less* |
INT_MAX | Maximum value for an object of type int | 32767 (215-1) or greater* |
UINT_MAX | Maximum value for an object of type unsigned int | 65535 (216-1) or greater* |
LONG_MIN | Minimum value for an object of type long int | -2147483647 (-231+1) or less* |
LONG_MAX | Maximum value for an object of type long int | 2147483647 (231-1) or greater* |
ULONG_MAX | Maximum value for an object of type unsigned long int | 4294967295 (232-1) or greater* |
LLONG_MIN | Minimum value for an object of type long long int | -9223372036854775807 (-263+1) or less* |
LLONG_MAX | Maximum value for an object of type long long int | 9223372036854775807 (263-1) or greater* |
ULLONG_MAX | Maximum value for an object of type unsigned long long int | 18446744073709551615 (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.h | climits | float.h和cfloat |
---|---|---|---|
使用numeric_limits<数据类型>::(min()ormax()) | Yes | No | No |
使用宏定义输出整型数据范围 | Yes | Yes | Yes |
使用宏定义输出浮点数数据范围 | No | No | Yes |