0 day streak
cpp / intermediate
Snippet

Portable Fixed-Width Integer Aliases

The <cstdint> header defines integer types with specific widths. Using types like uint64_t ensures your data structures have the same size across different operating systems and CPU architectures, which is critical for binary file formats and network protocols.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdint>
int main() {
// Guaranteed to be exactly 64 bits regardless of platform
uint64_t largeCounter = 18446744073709551615ULL;
// Guaranteed to be at least 16 bits
int_least16_t smallBuffer = -32768;
std::cout << "Size of uint64_t: " << sizeof(largeCounter) << " bytes" << std::endl;
return 0;
}
Breakdown
1
uint64_t
An unsigned integer type that is exactly 64 bits wide.
2
int_least16_t
The smallest signed integer type available that has at least 16 bits.