On 16/01/2020 08:44, Bo Persson wrote:
> If you limit yourself to Windows, this becomes a lot easier as you can
> just look up what the built in types are there - you can use short for
> 16 bits, int and unsigned for 32-bits, and long long when you need 64-bits.
>
I believe even MSVC, with its poor support for C99, has <stdint.h>.
There are /no/ advantages in using types like "short" and "long long"
over the standard sized types when you want specific sized types. If
you want a signed 16-bit type, you use "int16_t" - you don't write
something completely different ("short") and assume it matches, even if
that assumption is perfectly valid. This is about writing code in a way
that is clear - it cannot possibly be misinterpreted by any compiler, or
by any reader. If you write "long", the size can change between
platforms - and even on the one platform, programmers used to different
platforms might misread it. If you write "uint32_t", there is no
possibility of mistakes.
This is why these types were added to C - use them.
> The _t-types in the standard libary are for when *you* want to write a
> library for a wide set of systems and you don't know what type is 32
> bits wide (if any).
>
> One (very small) advantage of using int32_t is that it will not compile
> if you happen to try out your code on a 36-bit computer. But that never
> happens anyway if you use Windows.
>
A marginally more realistic advantage is that "int8_t" won't compile on
platforms with CHAR_BIT greater than 8 (and unlike 36-bit computers,
such processors are in wide use today - though not, obviously, running
Windows). And "int64_t" won't compile on platforms that don't support
64-bit types (for some limited compilers that don't support full C99).
In general, if you want a size-specific type, along with the additional
guarantees of two's complement integers with no padding bits, use the
size-specific types in <stdint.h>. Then you get what you ask for, the
code is clear, and you'll get a hard failure if the compiler can't give
you what you want.