Many sources, including Microsoft, reference both the int and long type as being 4 bytes and having a range of (signed) -2,147,483,648 to 2,147,483,647. What is the point of
No one has answered your actual question, except maybe AProgrammer.
The C/C++ standard is defined as Griwes described. This allows the C and the C++ language to be implemented where the compiler vendor can define the sizes most convenient to the computer architecture. For a while, (for Windows 3.1 and before, that is, before Windows 95), C code for windows had a 16 bit int whereas many UNIX platforms, such as Solaris, HPUX, and AIX had a 32 bit int.
However, modern microcomputers (since the 386), have full 32 bit registers, and addressing memory aligned to 32 bits is much faster then accessing 16 bit increments. Thus code is much more efficient with a 32 bit int, especially for int arrays, than it might be with a 16 bit int.
To simulate a 16 bit int in a 32 bit register, you also have to overflow at the 16th bit, instead of the 32nd. So it's just easier with a 32 bit int, even if you only have 32 bits available for long.
I think it's implementation dependent. They can vary, but it is up to the vendor to supply them. However, some vendors simply make it so that they are syntactically "supported" (ie: you can put them in your code and they will compile, but don't differ). Every so often, you'll encounter a language feature like this.
The only things guaranteed about integer types are:
sizeof(char) == 1
sizeof(char) <= sizeof(short)
sizeof(short) <= sizeof(int)
sizeof(int) <= sizeof(long)
sizeof(long) <= sizeof(long long)
sizeof(char) * CHAR_BIT >= 8
sizeof(short) * CHAR_BIT >= 16
sizeof(int) * CHAR_BIT >= 16
sizeof(long) * CHAR_BIT >= 32
sizeof(long long) * CHAR_BIT >= 64
The other things are implementation defined. Thanks to (4), both long
and int
can have the same size, but it must be at least 32 bits (thanks to (9)).
The C++ Language Specification simply states that the size of a long
must be at least the size of an int
.
It used to be standard to have int
= 2 bytes and long
= 4 bytes. For some reason int
grew up and long
stayed the same (on Windows compilers at least). I can only speculate that long
was kept the same for reasons of backwards compatibility...
It doesn't necesarily have to be larger. It's just the way the GCC, for example, usually has long
to be defined as 8 bytes when I've used it on my machine. The standard's wording usually says that these types 'Need to be at least X size' (for an example, check out the finally-standardized long long in C++11.
Essentially, anyone's free to do what they want as long as it meets the requirements. According to the standard, someone could make long long
256 bits, and it'd be perfectly legal.
C++ standard only specifies that long
is at least as big as int
, so there's nothing criminal in the scenario when it is exactly as big: it's totally implementation-defined. On different platforms, sizes may matter, for example I'm having int
of size 4 and long
of size 8 at the moment on my Linux machine.