limits.h
Content
整数值的最大值和最小值非常有用,或者简单地说,任何整数类型的极限在编程中都起着重要作用。无需记住这些值,可以使用不同的宏。**<climits>(limits.h)**定义整数类型的大小。[2]
- 此头定义了用于特定系统和编译器实现的基本整数类型的限制常量。
- 基本浮点类型的限制在 <cfloat> (<float.h>) 中定义。
- 宽度特定的整数类型和其他 typedef 类型的限制在 <cstdint> (<stdint.h>) 中定义。
| name[1] | expresses | possible 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* |
Demo
C
#include <stdio.h>
#include <limits.h>
int main() {
printf("The value of CHAR_BIT: %d\n", CHAR_BIT);
printf("The value of SCHAR_MIN: %d\n", SCHAR_MIN);
printf("The value of SCHAR_MAX: %d\n", SCHAR_MAX);
printf("The value of UCHAR_MAX: %u\n", UCHAR_MAX);
printf("The value of CHAR_MIN: %d\n", CHAR_MIN);
printf("The value of CHAR_MAX: %d\n", CHAR_MAX);
printf("The value of MB_LEN_MAX: %d\n", MB_LEN_MAX);
printf("The value of SHRT_MIN: %d\n", SHRT_MIN);
printf("The value of SHRT_MAX: %d\n", SHRT_MAX);
printf("The value of USHRT_MAX: %u\n", USHRT_MAX);
printf("The value of INT_MIN: %d\n", INT_MIN);
printf("The value of INT_MAX: %d\n", INT_MAX);
printf("The value of UINT_MAX: %u\n", UINT_MAX);
printf("The value of LONG_MIN: %ld\n", LONG_MIN);
printf("The value of LONG_MAX: %ld\n", LONG_MAX);
printf("The value of ULONG_MAX: %lu\n", ULONG_MAX);
return 0;
}
C++
// limits.cpp -- some integer limits
#include <iostream>
#include <climits> // use limits.h for older systems
int main()
{
using namespace std;
int n_int = INT_MAX; // initialize n_int to max int value
short n_short = SHRT_MAX; // symbols defined in climits file
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
// sizeof operator yields size of type or of variable
cout << "int is " << sizeof(int) << " bytes." << endl;
cout << "short is " << sizeof n_short << " bytes." << endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << endl;
cout << "Maximum values:" << endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong << endl
<< endl;
cout << "Minimum int value = " << INT_MIN << endl;
cout << "Bits per byte = " << CHAR_BIT << endl;
// cin.get();
return 0;
}
// (base) kimshan@MacBook-Pro output % ./"limits"
// int is 4 bytes.
// short is 2 bytes.
// long is 8 bytes.
// long long is 8 bytes.
// Maximum values:
// int: 2147483647
// short: 32767
// long: 9223372036854775807
// long long: 9223372036854775807
// Minimum int value = -2147483648
// Bits per byte = 8