On a system where both long and int is 4 bytes which is the best and why?
typedef unsigned long u32;
or
typedef unsigned int u32;
note: uint32_t is not an option
On a system where both long and int is 4 bytes which is the best and why?
typedef unsigned long u32;
or
typedef unsigned int u32;
note: uint32_t is not an option
Nowadays every platform has stdint.h
or its C++ equivalent cstdint
which define uint32_t
. Please use the standard type rather than creating your own.
The size will be the same between both, so it depend only on your use. If you need to store decimal values, use long.
A better and complete answer here: https://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132
Edit: I'm not sure about decimal with long, if someone can confirm, thanks.
Since you said the standard uint32_t
is not an option, using long
and int
are both correct on 32-bit machines, I'll say
typedef unsigned int u32;
is a little better, because on two popular 64-bit machine data models (LLP64 and LP64), int
is still 32-bit, while long
could be 32-bit or 64-bit. See 64-bit data models