C's Flexible Integer Sizes Were Not a Design Mistake
21 hours ago
- C beginners often assume integers have fixed sizes, but native C types (int, short, long) are platform-dependent in size.
- In the 1970s, C was designed for diverse hardware with varying word sizes (12–64 bits), byte sizes (6–9 bits), and number representations (two's complement, ones' complement, sign-magnitude).
- Fixed-size integers would have been inefficient or impractical on many architectures; flexible sizes enabled portability.
- The C standard guarantees minimum ranges (e.g., int at least 16 bits, long at least 32 bits) rather than exact sizes.
- <stdint.h> (C99) provides exact-width types like int32_t, but they are optional; the philosophy emphasizes minimum ranges.
- Lua source code demonstrates portable C techniques, such as using LUAI_IS32INT to check if int has at least 32 bits.
- Modern languages like Java, C#, Rust, Go, and Swift often fix integer sizes because hardware has converged, but still include platform-sized types (e.g., isize).
- C's flexible integers were not a design mistake; they were a key feature for portability across diverse machines.