Is it guaranteed that explicitly sized types (such as
uint16_t ) use the same amount of memory across all platforms (on desktops, on mobiles, on microcontrollers)? Actually, regardless of the answer, how these types can be used, are there any special tasks where you really need to know the size in bits? Masks, flags? Is it true that in modern C the usual wooden
int is under some kind of taboo and you should not use types whose size varies from platform to platform (references to the standard are important here, if it somehow regulates this or to compiler manuals)?
Answer 1, authority 100%
Explicitly Sized Types
The standard guarantees their size, but not their existence. That is, theoretically, you can find a platform that does not have the required type. But if the program has already been compiled, then you know the size of the type for sure.
There are also types with the word
least , for example,
uint_least32_t if you need at least 32 bits.
regular wooden int
The standard does not guarantee its size.
don’t use types that vary from platform to platform
It all depends on the tasks. If I need a 32-bit mask to iterate over, then it is logical not to rely on int. If some size, that is,
size_t , depending on the bitness of the program. And if I need to display the number of percentages, then I can take the usual
Answer 2, authority 27%
Types of the form
uintN_t must be exactly the same size in bits as
N . This is written in the Language Standard (example for
126.96.36.199 Exact-width integer types
1 The typedef name
intN_tdesignates a signed integer type with width
N, no padding bits, and a two’s complement representation. Thus,
int8_tdenotes such a signed integer type with a width of exactly
The exact size of the types is needed when different system components interact, which can run on different platforms. Because
sizeof (int) is not standardized and cannot be safely used in such cases. But if a variable of this type does not go beyond the boundaries of one system, then it is wiser to rely on basic types, like
For more information, see the similar C++ question
Answer 3, authority 27%
I am answering the question “why do we need fixed-size types?”
First, to transfer data structures over the network.
There are 2 programs (with the same source code, but each compiled in place) on two different systems and communicate with messages. Let, each message consists of 10 numbers of 4 bytes. But one system is 32 bits and the other is 64. Using int ? Flag in hand. Everything will break for obvious reasons.
Secondly, to work with different formats.
Pictures, archives, videos, binary configs of various programs have fixed structures. And if some field there is an unsigned integer of 4 bytes, then you need to use platform independent types.
There are many examples 🙂
The size of
int may vary on different platforms and may also depend on different compilers. And when using the same
uint32_t , you know exactly how much it takes. There was also such a situation that
Int was not enough. And the use of real types is not allowed. I had to use