hn.today

C's Flexible Integer Sizes Were Not a Design Mistake

pikuma.com5 points0 comments
Screenshot of C's Flexible Integer Sizes Were Not a Design Mistake

C's variable integer sizes were an intentional design choice to make the language map efficiently onto wildly different hardware of the 1960s-70s, not an oversight. Early machines used 12, 16, 18, 24, 36, 48, 60 and other bit word sizes, different character widths (6-9 bits), varying sign representations, and some even lacked byte-addressable memory. C evolved from BCPL and B, which treated values as machine words, and on the PDP-11 the language introduced types like char and int so programs could reflect the machine's "natural" word size. Porting UNIX to a 32-bit INTERDATA and the spread of portable compilers demonstrated that letting implementations choose efficient type widths allowed the same source to run natively across architectures.

The design trade-off favored guaranteed minimum ranges rather than exact widths: ANSI C specifies floors (char ≥8 bits, short ≥16, int ≥16, long ≥32, long long ≥64) while C99 added stdint.h for exact-width types when needed. Fixing int to 32 bits would have multiplied instruction counts and register use on smaller machines and broken assumptions on word-addressed systems where pointers and packed characters differed. Practical codebases like Lua exemplify the intended pattern - querying limits (e.g. UINT_MAX shifts) and selecting types by range - showing flexible sizes supported real-world portability and performance.

Read on pikuma.com0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.