I read that stdint.h
is used for portability, but I'm confused.
If I wrote a program on a 32-bit system, uint32_t (unsigned int) is 4-bytes.
But when this program is run on 16-bit system, int is 2bytes and uint32_t (unsigned int) is 2bytes.
I think portability is not guaranteed in this case. Is there anything I am understanding wrong?
CodePudding user response:
Is there any thing I am understanding wrong?
Yes.
Type uint32_t
is always an unsigned integer type with exactly 32 bits, none of them padding bits. On many modern systems that corresponds to type unsigned int
, but on others it might correspond to a different type, such as unsigned long int
. On systems that do not have a type with the correct properties, it is not defined at all.
The point of uint32_t
and the other explicit-width data types from stdint.h
is exactly to address the issue you raise, that (for example) unsigned int
has a different size on different machines.
CodePudding user response:
A uint32_t
and a uint16_t
are distinct types from int
.
While the size of int
may vary, a uint32_t
is always 32 bit and a uint16_t
is always 16 bit.