Fixed-size integers
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
The size of int and long depends on the platform: long is 8 bytes on Linux x86-64, but 4 bytes on Windows and in WebAssembly, where this site runs. When the size matters (file formats, networking, protocols, hardware), the types from <stdint.h> are used:
| type | size | range |
|---|---|---|
int8_t / uint8_t |
1 byte | -128…127 / 0…255 |
int16_t / uint16_t |
2 bytes | ±32,767 / 0…65,535 |
int32_t / uint32_t |
4 bytes | ±2·10⁹ / 0…4·10⁹ |
int64_t / uint64_t |
8 bytes | ±9·10¹⁸ / 0…1.8·10¹⁹ |
The macros from <inttypes.h> help you print them portably:
#include <inttypes.h>
uint64_t big = 18446744073709551615u;
printf("%" PRIu64 "\n", big);
PRIu64 is a string like "lu" or "llu", depending on the platform. C glues two strings next to each other into one.
Unsigned numbers "wrap around" modulo on overflow: uint8_t x = 255; x++; → 0. This is defined behaviour, and hash functions use it. But overflowing signed numbers is undefined behaviour.
Task. The FNV-1a hash function for a string: h = 2166136261; for each byte h ^= byte; h *= 16777619; in the type uint32_t (overflow here is part of the algorithm). Read a word and print its hash as an unsigned decimal number.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.