Cpp Libs Cstdint
`` is a header file introduced in C++11 that defines a set of fixed-width integer types, which have the same size and representation range on different platforms.
### Why Use ``
In C++, standard integer types (such as `int`, `long`, etc.) have sizes and representation ranges that depend on the compiler and platform. This can lead to inconsistent program behavior when compiled on different platforms. Using the fixed-width integer types defined in `` can avoid these problems because they have the same size and representation range on all platforms.
### Definitions and Syntax
`` defines the following integer types:
* `int8_t`: 8-bit signed integer
* `uint8_t`: 8-bit unsigned integer
* `int16_t`: 16-bit signed integer
* `uint16_t`: 16-bit unsigned integer
* `int32_t`: 32-bit signed integer
* `uint32_t`: 32-bit unsigned integer
* `int64_t`: 64-bit signed integer
* `uint64_t`: 64-bit unsigned integer
Additionally, maximum-width integer types are defined:
* `intmax_t`: Maximum-width signed integer
* `uintmax_t`: Maximum-width unsigned integer
And types for bit operations:
* `intptr_t`: Signed integer large enough to store pointer values
* `uintptr_t`: Unsigned integer large enough to store pointer values
## Example
Below is a simple example using ``, demonstrating how to declare and use these types.
## Example
#include
#include
int main(){
// Declare fixed-width integer types
int8_t a =-128;// Minimum value
uint8_t b =255;// Maximum value
int16_t c =-32768;
uint16_t d =65535;
int32_t e =-2147483648;
uint32_t f = 4294967295U;// Use U suffix to represent unsigned constant
int64_t g =-9223372036854775807LL;// Use LL suffix to represent long long constant
uint64_t h = 18446744073709551615ULL;// Use ULL suffix to represent unsigned long long constant
// Output results
std::cout<<"int8_t: "<<static_cast(a)<< std::endl;
std::cout<<"uint8_t: "<<static_cast(b)<< std::endl;
std::cout<<"int16_t: "<< c << std::endl;
std::cout<<"uint16_t: "<< d << std::endl;
std::cout<<"int32_t: "<< e << std::endl;
std::cout<<"uint32_t: "<< f << std::endl;
std::cout<<"int64_t: "<< g << std::endl;
std::cout<<"uint64_t: "<< h << std::endl;
return 0;
}
Output:
int8_t: -128uint8_t: 255int16_t: -32768uint16_t: 65535int32_t: -2147483648uint32_t: 4294967295int64_t: -9223372036854775807uint64_t: 18446744073709551615
* * *
## Fixed-Width Integer Types
Fixed-width integer types ensure that variables have a fixed width, which is very useful when precise control over data size and layout is required. They are named in the form `intN_t` and `uintN_t`, where `N` represents the bit width.
* `int8_t`, `int16_t`, `int32_t`, `int64_t`: Signed fixed-width integer types.
* `uint8_t`, `uint16_t`, `uint32_t`, `uint64_t`: Unsigned fixed-width integer types.
## Example
#include
#include
int main(){
int8_t a =-128;
uint8_t b =255;
int32_t c =2147483647;
uint64_t d = 18446744073709551615U;
std::cout<<"a = "<<static_cast(a)<< std::endl;
std::cout<<"b = "<<static_cast(b)<< std::endl;
std::cout<<"c = "<< c << std::endl;
std::cout<<"d = "<< d << std::endl;
return 0;
}
### Minimum Width Integer Types
Minimum width integer types ensure that variables have at least the specified bit width, which is very useful when a minimum bit width guarantee is needed but precise bit width control is not required. They are named in the form `int_leastN_t` and `uint_leastN_t`.
* `int_least8_t`, `int_least16_t`, `int_least32_t`, `int_least64_t`: Signed minimum width integer types.
* `uint_least8_t`, `uint_least16_t`, `uint_least32_t`, `uint_least64_t`: Unsigned minimum width integer types.
## Example
#include
#include
int main(){
int_least8_t a =-128;
uint_least8_t b =255;
int_least32_t c =2147483647;
uint_least64_t d = 18446744073709551615U;
std::cout<<"a = "<<static_cast(a)<< std::endl;
std::cout<<"b = "<<static_cast(b)<< std::endl;
std::cout<<"c = "<< c << std::endl;
std::cout<<"d = "<< d << std::endl;
return 0;
}
### Fastest Width Integer Types
Fastest width integer types ensure that variables have at least the specified bit width and are as fast as possible on the given system. They are named in the form `int_fastN_t` and `uint_fastN_t`.
* `int_fast8_t`, `int_fast16_t`, `int_fast32_t`, `int_fast64_t`: Signed fastest width integer types.
* `uint_fast8_t`, `uint_fast16_t`, `uint_fast32_t`, `uint_fast64_t`: Unsigned fastest width integer types.
## Example
#include
#include
int main(){
int_fast8_t a =-128;
uint_fast8_t b =255;
int_fast32_t c =2147483647;
uint_fast64_t d = 18446744073709551615U;
std::cout<<"a = "<<static_cast(a)<< std::endl;
std::cout<<"b = "<<static_cast(b)<< std::endl;
std::cout<<"c = "<< c << std::endl;
std::cout<<"d = "<< d << std::endl;
return 0;
}
### Special Types
* `intmax_t`: The largest signed integer type that can be represented.
* `uintmax_t`: The largest unsigned integer type that can be represented.
* `intptr_t`: A signed integer type large enough to store a pointer.
* `uintptr_t`: An unsigned integer type large enough to store a pointer.
## Example
#include
#include
int main(){
intmax_t max_int = INTMAX_MAX;
uintmax_t max_uint = UINTMAX_MAX;
intptr_t ptr_int =reinterpret_cast(&max_int);
uintptr_t ptr_uint =reinterpret_cast(&max_uint);
std::cout<<"max_int = "<< max_int << std::endl;
std::cout<<"max_uint = "<< max_uint << std::endl;
std::cout<<"ptr_int = "<< ptr_int << std::endl;
std::cout<<"ptr_uint = "<< ptr_uint << std::endl;
return 0;
}
### Limit Macros
`` also defines a set of macros for representing the limit values of these types.
* `INT8_MIN`, `INT8_MAX`, `UINT8_MAX`
* `INT16_MIN`, `INT16_MAX`, `UINT16_MAX`
* `INT32_MIN`, `INT32_MAX`, `UINT32_MAX`
* `INT64_MIN`, `INT64_MAX`, `UINT64_MAX`
## Example
#include
#include
int main(){
std::cout<<"INT8_MIN = "<<static_cast(INT8_MIN)<< std::endl;
std::cout<<"INT8_MAX = "<<static_cast(INT8_MAX)<< std::endl;
std::cout<<"UINT8_MAX = "<<static_cast(UINT8_MAX)<< std::endl;
std::cout<<"INT32_MIN = "<< INT32_MIN << std::endl;
std::cout<<"INT32_MAX = "<< INT32_MAX << std::endl;
std::cout<<"UINT32_MAX = "<< UINT32_MAX << std::endl;
return 0;
}
`` provides a set of fixed-width integer types that have the same size and representation range on different platforms. Using these types can improve code portability and predictability. When writing cross-platform C++ programs, it is recommended to use the types defined in ``.
YouTip