When should one use the datatypes from stdint.h? Is it right to always use as a convention them? What was the purpose of the design of nonspecific size types like int and short?
When should one use the datatypes from stdint.h?
- When the programming tasks specify the integer width especially to accommodate some file or communication protocol format.
- When high degree of portability between platforms is required over performance.
Is it right to always use as a convention them (then)?
Things are leaning that way. The fixed width types are a more recent addition to C. Original C had char, short, int, long
and that was progressive as it tried, without being too specific, to accommodate the various integer sizes available across a wide variety of processors and environments. As C is 40ish years old, it speaks to the success of that strategy. Much C code has been written and successfully copes with the soft integer specification size. With increasing needs for consistency, char, short, int, long and long long
, are not enough (or at least not so easy) and so int8_t, int16_t, int32_t, int64_t
are born. New languages tend to require very specific fixed integer size types and 2's complement. As they are successfully, that Darwinian pressure will push on C. My crystal ball says we will see a slow migration to increasing uses of fixed width types in C.
What was the purpose of the design of nonspecific size types like int and short?
It was a good first step to accommodate the wide variety of various integer widths (8,9,12,18,36, etc.) and encodings (2's, 1's, sign/mag). So much coding today uses power-of-2 size integers with 2's complement, that one may not realize that many other arrangements existed beforehand. See this answer also.
int
as the "most efficiently by the cpu" or more to the "native" CPU integer size. But this is not always so for some CPUs have a "native" 8-bit integer (Old CPUs and today's small embedded ones). But C requires at least 16 bits for an int
to be compliant. –
Socket My work demands that I use them and I actually love using them.
I find it useful when I have to implement a protocol and use them inside a structure which can be a message that needs to be sent out or a holder of certain information.
If I have to use a sequence number that needs to be incremented, I wouldn't use int because sequence numbers aren't supposed to be negative. I use uint32_t instead. I will hence know the sequence number space and can plan/code accordingly.
The code we write will be running on 32 as well as 64 bit machine so using "int" on different bit machines results in subtle bugs which can be a pain to identify. Using unint16_t will allocate 16 bits on 32 or 64 bit architecture.
uint32_t
on 32-bit systems uint32_t
on 64-bit systems, and int
can wreak havoc on code which doesn't use lots of explicit casts. Interactions among signed types tend to be more "sane". –
Tousle No, I would say it's never a good idea to use those for general-purpose programming.
If you really care about number of bits, then go ahead and use them but for most general use you don't care so then use the general types. The general types might be faster, and they are certainly easier to read and write.
uint32_t
etc just typedef
s? –
Slue int
) would suffice is going to slow the program down. –
Unfit uint_least8_t
or int_fast16_t
... If you wants portability, never use int or long, but use instead uint_leastXX_t
, int_fastXX_t
, or the classic uint32_t
–
Disaster uint32_t
, int32_t
, etc, are optional to be supplied by an implementation, per C99 7.20.1.1-3. Only the fast
and least
types are required, 8 of each (8,16,32,64, each signed and unsigned, are the only ones mandated). –
Onus Fixed width datatypes should be used only when really required (e.g. when implementing transfer protocols or accessing hardware or requiring a certain range of values (you should use the ..._least_...
variant there)). Your program won't adapt else on changed environments (e.g. using uint32_t
for filesizes might be ok 10 years ago, but off_t
will adapt to recent needs). As others have pointed out, there might be a performance impact as int
might be faster than uint32_t
on 16 bit platforms.
int
itself is very problematic due to its signedness; it is better to use e.g. size_t
when variable holds result of strlen()
or sizeof()
.
© 2022 - 2024 — McMap. All rights reserved.
int
was designed as the datatype which can be handled most efficiently by the cpu (e.g. a cpu register).long
was just longer andshort
shorter (but accessible as e.g. the half of a register (al
,ah
on x86)). – Repetend