C++ Standard Library<cstdint>
<cstdint>It is a header file introduced by C++11, which defines a set of fixed-width integer types that have the same size and representation range across different platforms.
Why Use<cstdint>
In C++, the standard integer types (such asint、longetc.) have sizes and representation ranges that depend on the compiler and platform. This can cause inconsistent behavior in programs compiled on different platforms. Using<cstdint>the fixed-width integer types defined in it can avoid these problems, because they have the same size and representation range on all platforms.
Definition and Syntax
<cstdint>The following integer types are defined:
int8_t: 8-bit signed integeruint8_t: 8-bit unsigned integerint16_t: 16-bit signed integeruint16_t: 16-bit unsigned integerint32_t: 32-bit signed integeruint32_t: 32-bit unsigned integerint64_t: 64-bit signed integeruint64_t: 64-bit unsigned integer
In addition, the maximum-width integer types are defined:
intmax_t: maximum-width signed integeruintmax_t: maximum-width unsigned integer
and the types used for bit operations:
intptr_t: a signed integer large enough to store the value of a pointeruintptr_t: an unsigned integer large enough to store the value of a pointer
Example
Below is an example of using<cstdint>A simple example of these types, demonstrating how to declare and use them.
Example
#include <cstdint>
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 denote unsigned constants
int64_t g = -9223372036854775807LL; // Use LL suffix to denote long long constants
uint64_t h = 18446744073709551615ULL; // Use ULL suffix to denote unsigned long long constants
// Output the results
std::cout << "int8_t: " << static_cast<int>(a) << std::endl;
std::cout << "uint8_t: " << static_cast<unsigned int>(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 result:
int8_t: -128 uint8_t: 255 int16_t: -32768 uint16_t: 65535 int32_t: -2147483648 uint32_t: 4294967295 int64_t: -9223372036854775807 uint64_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 needed. Their naming convention isintN_tanduintN_t, whereNIndicates the bit width.
int8_t,int16_t,int32_t,int64_t: signed fixed-width integer type.uint8_t,uint16_t,uint32_t,uint64_t: unsigned fixed-width integer type.
Example
#include <iostream>
int main() {
int8_t a = -128;
uint8_t b = 255;
int32_t c = 2147483647;
uint64_t d = 18446744073709551615U;
std::cout << "a = " << static_cast<int>(a) << std::endl;
std::cout << "b = " << static_cast<unsigned>(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 useful when a minimum bit-width guarantee is required but precise control over the bit width is not needed. Their naming convention isint_leastN_tanduint_leastN_t。
int_least8_t,int_least16_t,int_least32_t,int_least64_t: signed minimum-width integer type.uint_least8_t,uint_least16_t,uint_least32_t,uint_least64_t: unsigned minimum-width integer type.
Example
#include <iostream>
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<int>(a) << std::endl;
std::cout << "b = " << static_cast<unsigned>(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 a given system. Their naming convention isint_fastN_tanduint_fastN_t。
int_fast8_t,int_fast16_t,int_fast32_t,int_fast64_t: signed fastest-width integer type.uint_fast8_t,uint_fast16_t,uint_fast32_t,uint_fast64_t: unsigned fastest-width integer type.
Example
#include <iostream>
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<int>(a) << std::endl;
std::cout << "b = " << static_cast<unsigned>(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 <iostream>
int main() {
intmax_t max_int = INTMAX_MAX;
uintmax_t max_uint = UINTMAX_MAX;
intptr_t ptr_int = reinterpret_cast<intptr_t>(&max_int);
uintptr_t ptr_uint = reinterpret_cast<uintptr_t>(&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;
}
Boundary Value Macros
<cstdint>A set of macros is also defined to represent the boundary values of these types.
INT8_MIN,INT8_MAX,UINT8_MAXINT16_MIN,INT16_MAX,UINT16_MAXINT32_MIN,INT32_MAX,UINT32_MAXINT64_MIN,INT64_MAX,UINT64_MAX
Example
#include <iostream>
int main() {
std::cout << "INT8_MIN = " << static_cast<int>(INT8_MIN) << std::endl;
std::cout << "INT8_MAX = " << static_cast<int>(INT8_MAX) << std::endl;
std::cout << "UINT8_MAX = " << static_cast<unsigned>(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;
}
<cstdint>A set of fixed-width integer types is provided, which have the same size and representation range across different platforms. Using these types can improve code portability and predictability. When writing cross-platform C++ programs, it is recommended to use<cstdint>types defined in.