C's Flexible Integer Sizes Were Not a Design Mistake

(pikuma.com)

23 points | by ibobev 1 day ago

7 comments

  • adrian_b 2 minutes ago
    I agree that the C flexible integer sizes were still necessary at the time of its creation, when some important computers still had word sizes that were not powers of two.

    Nonetheless, I started to use C for programming only in 1990, when I got access to the Microsoft C and Borland Turbo C compilers.

    At that time, 36 years ago, the C flexible integer sizes were already obsolete.

    Since that time until now, while using C on a great variety of computers, from servers and workstations to the smallest microcontrollers, I have seen plenty of portability problems created by the existence of the flexible integer sizes.

    The only programs that had no portability problems were those that never used the flexible integer sizes, but only integers with a definite size, e.g. 8-bit, 16-bit, 32-bit or 64-bit.

  • pjmlp 5 minutes ago
    Kind of, the mistake was not doing like PL/I where besides default machine specific sizes, the developer could explicitly assert the required sizes.
  • flowerbreeze 33 minutes ago
    Thank you for the article! Do I see a Turbo-C screenshot there or am I imagining it? It was my first IDE (I didn't know that's what it was called) when I started programming. I sometimes miss it, it was really good, especially the help system.

    I agree with the article of course. I think most confusion comes not having learned about the purpose of having them be defined based on the architecture in the first place. It took me a long time before I stumbled upon how they really worked and why, because while I started it was either x86 or nothing. When x64 showed up, suddenly it became relevant and everybody started learning about C types more in depth as they ran into issues with sizeof.

    Also, misuse in data protocols is where I think the bad reputation of the flexible type sizes came from. stdint was desperately needed for that reason and it came a bit late.

  • quelsolaar 30 minutes ago
    Good article.

    C would probably not have survived unless it had this flexibility.

    But its not justa historical thing. Today there are modern platforms like DSPs that have 32bit sized char, because that is the smallest addressable type. These platforms depend on C for tool chains, even if most "portable" C wont run correctly on them. The fact that you can build hardware like that, and not have to invent a new language / dialect to program them is a huge win for the world.

    <edit> I didnt see the footnote about DSPs at first read </edit>

    • pjmlp 7 minutes ago
      C survived because UNIX carried it.
  • RobotToaster 16 minutes ago
    > A 'plain' int object has the natural size suggested by the architecture of the execution environment.

    Shouldn't they be 64 bits on most modern systems then?

    • flohofwoe 8 minutes ago
      It should indeed, and in hindsight it would have been better to move int to 64 bits (especially for C's integer promotion, which only really makes sense when the promotion happens to the register width.

      But porting 32-bit code to 64-bit was a big deal back then, and C99 with its new fixed-width integer types overlapped with the first AMD64 CPUs (and Microsoft's MSVC didn't start to support C99 until around 2015 anyway), I guess keeping int on 32-bits in the popular compilers was deemed 'safer' for porting existing code. I guess we can already be lucky that all the big compilers agreed on the same int width.

    • userbinator 5 minutes ago
      On x86-64, you need an extra prefix to do 64-bit operations (while 64-bit addressing is the default), so it's a question of "are you sure you need the 64 bits and 32 isn't enough?"
  • usrnm 44 minutes ago
    But not having fixed size integers (or integers tied to the size of a pointer) was. Both can be useful
    • quelsolaar 29 minutes ago
      At the time its was probably very hard to know what the fixed sizes should be.
      • pjmlp 6 minutes ago
        PL/I among other systems languages predating C, had the ability to explicitly define bit sizes for its types.
  • gustavopezzi 17 hours ago
    Author here. Thanks for sharing.
    • enriquto 43 minutes ago
      it's a great read!

      would like to learn some tricks, like configuring gcc so that int is 64 bits, and so on