<cstdint>
The <cstdint>
header is part of the C++ Standard Library and provides a set of typedefs for fixed-width integer types. This header ensures that you have access to integer types with specific sizes, regardless of the underlying platform or compiler. It's particularly useful when you need precise control over the size of integer types, which is common in systems programming, embedded systems, and when working with binary file formats or network protocols.
int8_t
, uint16_t
, int32_t
, uint64_t
)int_least8_t
, uint_least16_t
)int_fast8_t
, uint_fast32_t
)intmax_t
, uintmax_t
)#include <cstdint>
#include <iostream>
int main() {
int8_t small_int = 127;
uint16_t unsigned_short = 65535;
int32_t regular_int = -2147483648;
uint64_t big_unsigned = 18446744073709551615ULL;
std::cout << "int8_t: " << static_cast<int>(small_int) << std::endl;
std::cout << "uint16_t: " << unsigned_short << std::endl;
std::cout << "int32_t: " << regular_int << std::endl;
std::cout << "uint64_t: " << big_unsigned << std::endl;
return 0;
}
int8_t
, uint16_t
, int32_t
, and uint64_t
.small_int
to int
when printing, as cout
treats int8_t
as a character.ULL
suffix is used for the uint64_t
literal to ensure it's treated as an unsigned long long.#include <cstdint>
#include <iostream>
struct NetworkPacket {
uint_least8_t type;
uint_least16_t length;
uint_least32_t sequence_number;
};
void print_packet(const NetworkPacket& packet) {
std::cout << "Packet Type: " << static_cast<int>(packet.type) << std::endl;
std::cout << "Packet Length: " << packet.length << std::endl;
std::cout << "Sequence Number: " << packet.sequence_number << std::endl;
}
int main() {
NetworkPacket packet = {5, 1024, 123456789};
print_packet(packet);
return 0;
}
uint_least8_t
, uint_least16_t
, uint_least32_t
) to ensure portability.packet.type
to int
when printing to ensure it's displayed as a number, not a character.#include <cstdint>
#include <iostream>
#include <chrono>
uint_fast32_t fibonacci(uint_fast8_t n) {
if (n <= 1) return n;
uint_fast32_t a = 0, b = 1;
for (uint_fast8_t i = 2; i <= n; ++i) {
uint_fast32_t temp = a + b;
a = b;
b = temp;
}
return b;
}
int main() {
const int iterations = 1000000;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
fibonacci(30);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time to calculate fibonacci 1,000,000 times: " << diff.count() << " s" << std::endl;
return 0;
}
uint_fast8_t
for the function parameter and loop counter, as it's the fastest type for small integers.uint_fast32_t
is used for the Fibonacci numbers, as it's the fastest type that can hold the result.#include <cstdint>
#include <iostream>
#include <iomanip>
int main() {
std::cout << "Integer Limits:" << std::endl;
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<int>(UINT8_MAX) << std::endl;
std::cout << "INT16_MIN: " << INT16_MIN << std::endl;
std::cout << "INT16_MAX: " << INT16_MAX << std::endl;
std::cout << "UINT16_MAX: " << UINT16_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;
std::cout << "INT64_MIN: " << INT64_MIN << std::endl;
std::cout << "INT64_MAX: " << INT64_MAX << std::endl;
std::cout << "UINT64_MAX: " << UINT64_MAX << std::endl;
std::cout << "\nConstant Macros:" << std::endl;
std::cout << "INT8_C(127): " << static_cast<int>(INT8_C(127)) << std::endl;
std::cout << "UINT16_C(65535): " << UINT16_C(65535) << std::endl;
std::cout << "INT32_C(-2147483648): " << INT32_C(-2147483648) << std::endl;
std::cout << "UINT64_C(18446744073709551615): " << UINT64_C(18446744073709551615) << std::endl;
return 0;
}
INT8_MIN
, UINT16_MAX
) for each integer type.INT8_C
, UINT64_C
) which ensure the correct type for integer constants.static_cast<int>
for 8-bit types to ensure proper display as integers.Portability: Using these fixed-width types ensures consistent behavior across different platforms and compilers.
Performance: While fixed-width types guarantee size, they may not always be the most efficient on all platforms. Use fast
types when performance is critical.
Overflow Behavior: Be aware that overflow behavior is still undefined for signed types. Always check for potential overflows in your code.
Printing: When printing 8-bit types, cast them to int
to avoid them being treated as characters.
Compatibility: These types are compatible with C, making them useful in C/C++ interop scenarios.
The <cstdint>
header in C++ provides a set of portable integer types with guaranteed sizes:
intN_t
, uintN_t
) offer precise control over integer sizes.int_leastN_t
, uint_leastN_t
) guarantee at least N bits.int_fastN_t
, uint_fastN_t
) provide the most efficient type with at least N bits.intmax_t
, uintmax_t
) represents the largest available integer type.These types are essential for writing portable code, especially in systems programming, embedded systems, and when dealing with binary data or network protocols. By using <cstdint>
, developers can ensure consistent integer sizes across different platforms and compilers, leading to more robust and portable code.