📅  最后修改于: 2020-12-19 05:37:39             🧑  作者: Mango
limits.h标头确定各种变量类型的各种属性。此标头中定义的宏限制了各种变量类型(例如char,int和long)的值。
这些限制指定变量不能存储超出这些限制的任何值,例如,无符号字符最多可以存储255。
以下值为特定于实现的值,并使用#define指令定义,但这些值不得低于此处给出的值。
Macro | Value | Description |
---|---|---|
CHAR_BIT | 8 | Defines the number of bits in a byte. |
SCHAR_MIN | -128 | Defines the minimum value for a signed char. |
SCHAR_MAX | +127 | Defines the maximum value for a signed char. |
UCHAR_MAX | 255 | Defines the maximum value for an unsigned char. |
CHAR_MIN | -128 | Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero. |
CHAR_MAX | +127 | Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX. |
MB_LEN_MAX | 16 | Defines the maximum number of bytes in a multi-byte character. |
SHRT_MIN | -32768 | Defines the minimum value for a short int. |
SHRT_MAX | +32767 | Defines the maximum value for a short int. |
USHRT_MAX | 65535 | Defines the maximum value for an unsigned short int. |
INT_MIN | -2147483648 | Defines the minimum value for an int. |
INT_MAX | +2147483647 | Defines the maximum value for an int. |
UINT_MAX | 4294967295 | Defines the maximum value for an unsigned int. |
LONG_MIN | -9223372036854775808 | Defines the minimum value for a long int. |
LONG_MAX | +9223372036854775807 | Defines the maximum value for a long int. |
ULONG_MAX | 18446744073709551615 | Defines the maximum value for an unsigned long int. |
以下示例显示了limits.h文件中定义的几个常量的用法。
#include
#include
int main() {
printf("The number of bits in a byte %d\n", CHAR_BIT);
printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX);
printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
printf("The maximum value of SHORT INT = %d\n", SHRT_MAX);
printf("The minimum value of INT = %d\n", INT_MIN);
printf("The maximum value of INT = %d\n", INT_MAX);
printf("The minimum value of CHAR = %d\n", CHAR_MIN);
printf("The maximum value of CHAR = %d\n", CHAR_MAX);
printf("The minimum value of LONG = %ld\n", LONG_MIN);
printf("The maximum value of LONG = %ld\n", LONG_MAX);
return(0);
}
让我们编译并运行上面的程序,它将产生以下结果-
The number of bits in a byte 8
The minimum value of SIGNED CHAR = -128
The maximum value of SIGNED CHAR = 127
The maximum value of UNSIGNED CHAR = 255
The minimum value of SHORT INT = -32768
The maximum value of SHORT INT = 32767
The minimum value of INT = -2147483648
The maximum value of INT = 2147483647
The minimum value of CHAR = -128
The maximum value of CHAR = 127
The minimum value of LONG = -9223372036854775808
The maximum value of LONG = 9223372036854775807