cpp / intermediate
Snippet
Precise Fixed-Width Integer Aliases
Standard C++ integer types like 'int' or 'long' can vary in size between architectures. The <cstdint> header provides aliases like int32_t or uint64_t that guarantee a specific bit-width, which is essential for cross-platform data structures and binary formats.
snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>#include <cstdint>#include <limits>int main() {// Guaranteed 64-bit unsigned integerstd::uint64_t largeValue = 1000000000000ULL;// Guaranteed 8-bit signed integerstd::int8_t smallValue = 120;std::cout << "Size of uint64_t: " << sizeof(largeValue) << " bytes\n";std::cout << "Max int8_t: " << (int)std::numeric_limits<std::int8_t>::max() << "\n";return 0;}
Breakdown
1
std::uint64_t largeValue = 1000000000000ULL;
Declares an unsigned 64-bit integer, ensuring it holds 8 bytes regardless of the platform.
2
std::int8_t smallValue = 120;
Declares a signed 8-bit integer, typically used for small numbers or character-like byte data.