输出C++基本数据类型的数据范围
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
bool1⽆fal 或 true
char1signed char–128 到 127
unsigned char1⽆0 到 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*⽆由操作系统决定,即与操作系统的"字长"有关
float4⽆ 3.4E +/- 38 (7 digits)
double8⽆ 1.7E +/- 308 (15 digits)
long double8⽆ 1.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_ba
{
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';
qq空间网址
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';
return0;
}
或输出宏定义
#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';
return0;
}
可输出的宏定义如下:
name express 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 char127 (27-1) or greater* UCHAR_MAX Maximum value for an object of type unsigned char255 (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*
name express
value 可以看到,基本整形数据类型的最⼤,最⼩值都有相应宏定义,不过⽆符号数的最⼩值都没有定义,⼤概是默认为零,不作定义;同样没有定义的还有浮点数类型。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定义了浮点数数据类型的范围
使⽤它们作为头⽂件,即可输出浮点数类型的范围,输出整型数据类型的范围也是可以的:
4. 总结(软件为dev C++):
\
limits.h climits float.h和cfloat
初九使⽤numeric_limits<;数据类型>::(min()ormax())四年级下册语文第一课古诗三首
Yes No No 使⽤宏定义输出整型数据范围Yes Yes Yes 使⽤宏定义输出浮点数数据范围
No
No
Yes
#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;}