Why does sizeof(int) vary across different operating systems?

I wounder why size of int depends on which OS one is using ,in C & C++. It's ok if size of pointer varies, but why size of integer. If 16 bit OS sizeof(int) = 2 byte, for 32 bit sizeof(int) = 4 byte. Why so?

Thanks.


Why so?

Historical reasons.

Before the advent of the ANSI C standard and size_t in 1989, int was the type used to index into arrays. malloc took an int as its argument, strlen returned one. Thus int had to be large enough to index any array, but small enough to not cause too much overhead. For file offsets, typically a larger type such as long was typedef'd to off_t.

On the PDP-11 were C was first implemented in the early 1970s, int was as large as a processor register: 16 bits. On larger machines such as the VAX, it was widened to 32 bits to allow for larger arrays.

This convention has been largely abandoned; the C and C++ standards use size_t and ssize_t for indices and lenghts of arrays. On 64-bit platforms, often int is still 32 bits wide while size_t is 64 bits. (Many older APIs, e.g. CBLAS, still use int for indices, though.)


According to the C++ standard

1.7.1 states:

The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set ...

then 3.9.1.1 states:

Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set.

So we can infer that char is actually a byte. Most importantly 3.9.1.2 also says:

There are five signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs.

So in other words the size of int is (a) guaranteed to be at least a byte and (b) naturally aligned to the OS/hardware it's running on so most likely these days to be 64 bit or (for many older systems) 32 bit.


A byte is the smallest unit of memory which your target system can handle and uniquely address. As such, the size of a byte is platform and compiler-dependant, but in most settings is 8 bits.

So, assuming a byte is 8 bits, this would mean that 64 bits equals 8 bytes, 32 bits equals 4 bytes and 16 bits equals 2 bytes.

On a fuzzy level, an "X bit system" is a system where basic values (registers, ints, etc.) are X bits by default. Thus, how many bits your system natively uses immediately affects how many bytes are needed to hold those values.