In the C99 Standard, 7.18.1.3 Fastest minimum-width integer types.
(7.18.1.3p1) "Each of the following types designates an integer type that is usually fastest225) to operate with among all integer types that have at least the specified width."
225) "The designated type is not guaranteed to be fastest for all purposes; if the implementation has no clear grounds for choosing one type over another, it will simply pick some integer type satisfying the signedness and width requirements."
and
(7.18.1.3p2) "The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N. The typedef name uint_fastN_t designates the fastest unsigned integer
type with a width of at least N."
The types int_fastN_t
and uint_fastN_t
are counterparts to the exact-width integer types intN_t
and uintN_t
. The implementation guarantees that they take at least N
bits, but the implementation can take more bits if it can perform optimization using larger types; it just guarantees they take at least N
bits.
For example, on a 32-bit machine, uint_fast16_t
could be defined as an unsigned int
rather than as an unsigned short
because working with types of machine word size would be more efficent.
Another reason of their existence is the exact-width integer types are optional in C but the fastest minimum-width integer types and the minimum-width integer types (int_leastN_t
and uint_leastN_t
) are required.
what's the difference with uint32_t
uint_fast32_t
is an unsigned type of at least 32 bits that is (in some general way) the fastest such type. "fast" means that given a choice, the implementer will probably pick the size for which the architecture has arithmetic, load and store instructions. It's not the winner of any particular benchmark.
uint_least32_t
is the smallest unsigned type of at least 32 bits.
uint32_t
is a type of exactly 32 bits with no padding, if any such type exists.
Am I right?
No. If uint24_t
exists at all then it is an integer type, not a struct
. If there is no unsigned integer type of 24 bits in this implementation then it does not exist.
Since unsigned long
is required to be at least 32 bits, the only standard types that uint24_t
could possibly ever be an alias for are char
, unsigned char
, unsigned short
and unsigned int
. Alternatively it could be an extended type (that is, an integer type provided by the implementation that is not any of the defined integer types in the standard).
Will you suggest me to use uint48_t for my 48-bit unsigned integers?
If it exists and is the size you want then you might as well use it. However, it will not exist on very many implementations, so it's only suitable for use in non-portable code. That's OK provided the reason you have to deal with exact 48-bit integers is platform-specific.
The exact 16, 32 and 64 bit types are also technically optional, but they are required to exist if the implementation has suitable integer types. "Suitable" means not only that there is an exact N bit unsigned type with no padding bits, but also that the corresponding signed type has no padding bits and uses 2's complement representation. In practice this is so close to everywhere that you limit portability very little by using any of them. For maximum portability though you should use uint_least32_t
or uint_fast32_t
in preference to uint32_t
. Which one depends on whether you care more about speed or size. In my experience very few people bother, since platforms that don't have a 32 bit integer type are already so weird that most people don't care whether their code runs on it or not.
Best Answer
int
may be as small as 16 bits on some platforms. It may not be sufficient for your application.uint32_t
is not guaranteed to exist. It's an optionaltypedef
that the implementation must provide iff it has an unsigned integer type of exactly 32-bits. Some have a 9-bit bytes for example, so they don't have auint32_t
.uint_fast32_t
states your intent clearly: it's a type of at least 32 bits which is the best from a performance point-of-view.uint_fast32_t
may be in fact 64 bits long. It's up to the implementation.uint_least32_t
in the mix. It designates the smallest type that's at least 32 bits long, thus it can be smaller thanuint_fast32_t
. It's an alternative touint32_t
if the later isn't supported by the platform.What you are looking at is not the standard. It's a particular implementation (BlackBerry). So you can't deduce from there that
uint_fast32_t
is always the same asuint32_t
.See also:
Exotic architectures the standards committees care about.
My pragmatic opinion about integer types in C and C++.