Skip to content
HN On Hacker News ↗

C's Flexible Integer Sizes were Not a Design Mistake

▲ 113 points • 166 comments • by ibobev • 2w ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this text is a mix of AI and human-written content.

79 %

AI likelihood · overall

AI
17% human-written 83% AI-generated
SEGMENTS · HUMAN 1 of 2
SEGMENTS · AI 1 of 2
WORD COUNT 406
PEAK AI % 97% · §1
Analyzed
Sep 27
backend: pangram/v3.3
Segments scanned
2 windows
avg 203 words each
Distribution
17 / 83%
human / AI fraction
Verdict
AI
Pangram v3.3

Article text · 406 words · 2 segments analyzed

Human AI-generated
§1 AI · 97%

Word sizes, strange machines, and why int was never meant to mean 32 bits. C's flexible integer types were not a design mistake. They were how the language achieved portability in a world of 12, 18, 36, and 60-bit computers.

§2 Human · 9%

Here at the school, we work with a lot of retro machines and old game consoles, so it's no surprise that we are often the first point of contact for many developers learning the C programming language. Many of our students come come from languages like Python, JavaScript, Java, C#, Swift, and others. It does not take long for beginners to realize that many aspects they took for granted in their primary language are not necessarily a given in C. Fairly often, we find ourselves asking questions such as: How many bits does this variable occupy in memory? What is the padding in this structure? Is this field properly aligned in RAM? Integers are Not 32-bits Another rite of passage for C beginners is to learn how to use and to make sense of the operator sizeof. Observing their program output 4 when they ask for the sizeof(int) seems reasonable enough, given that in most modern machines, a signed integer occupies, indeed, 4 bytes in memory. sizeof int: 4 bytes Unfortunately, I must also tell my students that, when I first started learning how to code, if I asked for the sizeof(int), my old 386 machine would output 2 bytes! sizeof int: 2 bytes And right about here is where we start a discussion on how native C integer types do not have a fixed size. Language types such as char, int, short, and long do not come with a guarantee of how many bytes they occupy in memory. Since we usually want fixed sizes in our code, we tend to suggest students to use the STDINT.H header to take advantage of actual fixed-size types, such as: int8_t: signed integers of exactly 8 bits uint8_t: unsigned integers of exactly 8 bits int16_t: signed integers of exactly 16 bits uint16_t: unsigned integers of exactly 16 bits int32_t: signed integers of exactly 32 bits uint32_t: unsigned integers of exactly 32 bits etc. The header <STDINT.H> was introduced as part of the C99 standard (published in 1999), and it helps programmers guarantee that their variables are declared to occupy an exact number of of bytes, regardless of the compiler and regardless of the machine architecture we are compiling against.