‹ BackHN Continuity

Thread

C's Flexible Integer Sizes Were Not a Design Mistake

113 points · 166 comments · ibobev

  1. RobotToaster · · focus · HN ↗
    > 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?

    1. entrope · · focus · HN ↗
      > Shouldn't they be 64 bits on most modern systems then?

      Arguably so, but then one would lose the ability to natively name 16-bit integer types because "short" would be 32 bits.

      An earlier comment addresses x86-64. AArch64 (pedantically, the A64 instruction set used for AArch64&#x27;s 64-bit execution mode) is similar, in that addresses are 64 bits wide but ALU instructions typically encode a width bit, called &quot;sf&quot;, that selects either 32- or 64-bit data registers and arithmetic. See, for example, <a href="https:&#x2F;&#x2F;arm.jonpalmisc.com&#x2F;latest_aarch64&#x2F;add_addsub_ext" rel="nofollow">https:&#x2F;&#x2F;arm.jonpalmisc.com&#x2F;latest_aarch64&#x2F;add_addsub_ext .

      1. imtringued · · focus · HN ↗
        char is at least 8 bits, short is at least 16 bits, long is at least 32 bits, long long is at least 64 bits.

        Not sure how what you said makes sense.

        1. debugnik · · focus · HN ↗
          long can&#x27;t be smaller than int, so a 64-bit int leaves short as the only type between char and long long, and you need to pick whether it&#x27;s 16 bits or 32.

          Then again, we&#x27;d still have stdint around. And ultimately this doesn&#x27;t matter because most code isn&#x27;t portable anyway.

          1. matvore · · focus · HN ↗
            You would still want a native 16 bit type in order to have a pointer to a 16-bit memory location, including an array of 16-bit values.
            1. debugnik · · focus · HN ↗
              I don&#x27;t think of the stdint types as being less native than the keyword integer types. C# for example makes keyword types aliases to System.* types, not the other way around.
              1. matvore · · focus · HN ↗
                I think you would need to have a compiler intrinsic type which is 16 bits. That is what the stdint.h file would define (u)int_16 to.

                It would be odd for there to be a compiler intrinsic type to be unavailable until a header was included.

                The compiler intrinsic type could be a mess like __int16_exactly_t but without it stdint.h would have some magic line which makes a compiler intrinsic available which wasn&#x27;t before, or generates a new 16-bit type ex nihilo.

                So you could have a &quot;#pragma expose_extra_types&quot; in stdint.h but that would not be the conventional approach.

                I mostly wanted to make the point that a mere &quot;at least 16 bits&quot; type is not sufficient for some use cases, which is not in response to you but the comment you responded to.

                1. sparkie · · focus · HN ↗
                  Yeah, in GCC for example, we can define exact width types without including `&lt;stdint.h&gt;`.

                      typedef unsigned __attribute__((mode(HI))) uint16_t;
                      typedef signed __attribute__((mode(SI))) int32_t;
                  
                  Of course, the mode needs to be supported by the compiler for the target arch, but we don&#x27;t need to include anything.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.