limits头文件使用

limits头文件使用,第1张

c++中提供了一个limits库,可以直接得到各数值类型的最大、最小值。

这里简略记录下以方便后续查阅。使用方式如下:

using namespace std

int main(){

// int32

cout<<INT_MAX<<endl

cout<<INT_MIN<<endl

cout<<UINT_MAX<<endl

cout<<UINT_MIN<<endl

cout<<LONG_MAX<<endl

cout<<LONG_MIN<<endl

}

int最大长度是11位。在32位的机器下,int的范围是 - 2 ^ 31 ~2 ^ 31 - 1 也就是:[-2147483648, 2147483647];在16位的机器下,int的范围为 -2 ^ 15 ~ 2 ^ 15-1。

也就是 :[-32768,32767];整型变量int的范围与计算机的字长有关,int 的长度是一个计算机字长。

int类型允许存储的字节数是4个字节,换算出int UNSIGNED(无符号)类型的能存储的最小值为0,最大值为4294967295(即4B=32b, 最大值即为32个1组成)。

扩展资料:

int型字长问题:

long int型至少应该和int型一样长,而int型至少应该和short int一样长。

1、C/C++规定int字长和机器字长相同;

2、 *** 作系统字长和机器字长未必一致;

3、编译器根据 *** 作系统字长来定义int字长;

在一些没有 *** 作系统的嵌入式计算机系统上,int的长度与处理器字长一致;有 *** 作系统时, *** 作系统的字长与处理器的字长不一定一致,此时编译器根据 *** 作系统的字长来定义int字长:“比如在64位机器上运行DOS16系统。

那么所有for dos16的C/C++编译器中int都是16位的;在64位机器上运行win32系统,那么所有for win32的C/C++编译器中int都是32位的”。(CPU的“字长”是指其一条指令/一次运算可以处理的数据的最大宽度。

对于整型的数值范围,每个编译器里面都有一个标准头文件:limits.h,这个头文件定义了一些宏,这些宏表示该编译器使用的所有数据类型的范围,编程过程中使用这些宏就行了。

头文件的库函数。我给你一个路径。==

D:\Microsoft Visual Studio\VC98\Include

你就是找这个include文件下就可以了。

limits.h

MSDN

中的解释。

The limits for integer types are listed in the following table. These limits are also defined in the standard header file LIMITS.H.

Limits on Integer Constants

Constant Meaning Value

CHAR_BIT Number of bits in the smallest variable that is not a bit field. 8

SCHAR_MIN Minimum value for a variable of type signed char. –128

SCHAR_MAX Maximum value for a variable of type signed char. 127

UCHAR_MAX Maximum value for a variable of type unsigned char. 255 (0xff)

CHAR_MIN Minimum value for a variable of type char. –1280 if /J option used

CHAR_MAX Maximum value for a variable of type char. 127255 if /J option used

MB_LEN_MAX Maximum number of bytes in a multicharacter constant. 2

SHRT_MIN Minimum value for a variable of type short. –32768

SHRT_MAX Maximum value for a variable of type short. 32767

USHRT_MAX Maximum value for a variable of type unsigned short. 65535 (0xffff)

INT_MIN Minimum value for a variable of type int. –2147483647 – 1

INT_MAX Maximum value for a variable of type int. 2147483647

UINT_MAX Maximum value for a variable of type unsigned int. 4294967295 (0xffffffff)

LONG_MIN Minimum value for a variable of type long. –2147483647 – 1

LONG_MAX Maximum value for a variable of type long. 2147483647

ULONG_MAX Maximum value for a variable of type unsigned long. 4294967295 (0xffffffff)

If a value exceeds the largest integer representation, the Microsoft compiler generates an error.

END Microsoft Specific


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11594928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存