To answer your direct question: There is no guaranteed relationship between the natural word-size of the processor and the C and C++ types int
or long
. Yes, quite often int
will be the same as the size of a register in the processor, but most 64-bit processors do not follow this rule, as it makes data unnecessarily large. On the other hand, an 8-bit processor would have a register size of 8 bits, but int
according to the C and C++ standards needs to be at least 16 bits in size, so the compiler would have to use more than one register to represent one integer [in some fashion].
In general, if you want to KNOW how many bits or bytes some type is, it's best to NOT rely on int
, long
, size_t
or void *
, since they are all likely to be different for different processor architectures or even different compilers on the same architecture. An int
or long
may be the same size or different sizes. Only rule that the standard says is that long
is at least 32 bits.
So, to have control of the number of bits, use #include <cstdint>
(or in C, stdint.h
), and use the types for example uint16_t
or uint32_t
- then you KNOW that it will hold a given number of bits.
On a processor that has 36-bit "wordsize", the type uint32_t
for example, will not exist, since there is no type that holds exactly 32-bits [most likely]. Alternatively, the compiler may add extra instructions to "behave as if it's a 32-bit type" (in other words, sign extending if necessary, and masking off the top bits as needed)