The C++ standard library
is a header file in the C++ standard library that provides limits and characteristics related to integer types. It defines a set of constants describing various integer types (such as char, int, long ) minimum, maximum, and other related attributes. These constants come from the C standard library's header file in your C++ program.
Constants provided by
These constants describe the characteristics of different integer types on a specific platform. Here are some commonly used constants:
- Character type
CHAR_BIT:char: The number of bits of the type (usually 8).CHAR_MIN:char: The minimum value of the type.CHAR_MAX:char: The maximum value of the type.SCHAR_MIN: Signedchar: The minimum value of the type.SCHAR_MAX: Signedchar: The maximum value of the type.UCHAR_MAX: Unsignedchar: The maximum value of the type.
- Short integer
SHRT_MIN:short: The minimum value of the type.SHRT_MAX:short: The maximum value of the type.USHRT_MAX: Unsignedshort: The maximum value of the type.
- Integer
INT_MIN:int: The minimum value of the type.INT_MAX:int: The maximum value of the type.UINT_MAX: Unsignedint: The maximum value of the type.
- Long integer
LONG_MIN:long: The minimum value of the type.LONG_MAX:long: The maximum value of the type.ULONG_MAX: Unsignedlong: The maximum value of the type.
- Long long integer
LLONG_MIN:long long: The minimum value of the type.LLONG_MAX:long long: The maximum value of the type.ULLONG_MAX: Unsignedlong long: The maximum value of the type.
Example
Below is an example using example program of constants defined in the header:
Example
#include
#include
int
main
(
)
{
// Print the max and min values of int
std
::
cout
<<
"int max value: "
<<
INT_MAX
<<
std
::
endl
;
std
::
cout
<<
"int min value: "
<<
INT_MIN
<<
std
::
endl
;
// Print the max and min values of long
std
::
cout
<<
"long max value: "
<<
LONG_MAX
<<
std
::
endl
;
std
::
cout
<<
"long min value: "
<<
LONG_MIN
<<
std
::
endl
;
// Print the max value of unsigned long
std
::
cout
<<
"unsigned long max value: "
<<
ULONG_MAX
<<
std
::
endl
;
// Print the max and min values of char
std
::
cout
<<
"char max value: "
<<
CHAR_MAX
<<
std
::
endl
;
std
::
cout
<<
"char min value: "
<<
CHAR_MIN
<<
std
::
endl
;
return
0
;
}
Output result
When you run the program above, the output will be similar to the following (specific values may vary by compiler and platform):
int 的最大值是:2147483647
int 的最小值是:-2147483648
long 的最大值是:9223372036854775807
long 的最小值是:-9223372036854775808
unsigned long 的最大值是:18446744073709551615
char 的最大值是:127
char 的最小值是:-128
The constants in the header provide useful information about integer type representations, enabling programmers to write platform-independent code and ensure consistent behavior across platforms. Understanding the meaning and usage of these constants is especially important for applications requiring high precision and range control. If you have specific usage needs or questions, we can discuss further.