Would the size of an integer depend upon the compiler, OS and processor?
Yes, it depends on both processors (more specifically, ISA, instruction set architecture, e.g., x86 and x86-64) and compilers including programming model. For example, in 16-bit machines, sizeof (int) was 2 bytes. 32-bit machines have 4 bytes for int
. It has been considered int
was the native size of a processor, i.e., the size of register. However, 32-bit computers were so popular, and huge number of software has been written for 32-bit programming model. So, it would be very confusing if 64-bit computer would have 8 bytes for int
. Both Linux and Windows remain 4 bytes for int
. But, they differ in the size of long
.
Please take a look at the 64-bit programming model like LP64 for most *nix and LLP64 for Windows:
Such differences are actually quite embarrassing when you write code that should work both on Window and Linux. So, I'm always using int32_t
or int64_t
, rather than long
, via stdint.h.