looking for something completely unrelated and stumbled across this and needed to answer. Yeah, this is old, so for people who surf on in later...
Frankly, I think all the answers on here are incomplete.
The size of a long is the size of the number of bits your processor can operate on at one time. It's also called a "word". A "half-word" is a short. A "doubleword" is a long long and is twice as large as a long (and originally was only implemented by vendors and not standard), and even bigger than a long long is a "quadword" which is twice the size of a long long but it had no formal name (and not really standard).
Now, where does the int come in? In part registers on your processor, and in part your OS. Your registers define the native sizes the CPU handles which in turn define the size of things like the short and long. Processors are also designed with a data size that is the most efficient size for it to operate on. That should be an int.
On todays 64bit machines you'd assume, since a long is a word and a word on a 64bit machine is 64bits, that a long would be 64bits and an int whatever the processor is designed to handle, but it might not be. Why? Your OS has chosen a data model and defined these data sizes for you (pretty much by how it's built). Ultimately, if you're on Windows (and using Win64) it's 32bits for both a long and int. Solaris and Linux use different definitions (the long is 64bits). These definitions are called things like ILP64, LP64, and LLP64. Windows uses LLP64 and Solaris and Linux use LP64:
Model ILP64 LP64 LLP64
int 64 32 32
long 64 64 32
pointer 64 64 64
long long 64 64 64
Where, e.g., ILP means int-long-pointer, and LLP means long-long-pointer
To get around this most compilers seem to support setting the size of an integer directly with types like int32 or int64.
int
be the "natural" size for the processor, which may not always be as big as along
. The standard also guarantees that along
is at least as long as anint
, so the fact that they are equal sizes are not always guaranteed. – Summarizeint
andlong
always being the same size; I am currently fixing a ton of portability issues in one of our C++ static libraries as we transition to a 64-bit architecture – Fourteenthsizeof(long) == sizeof(int)
only on 32-bit architectures or 64-bit Windows, where it comes as a shock to programmers who are used tosizeof(long) == sizeof(void *)
. – Onachar
,short
,long
, andlong long
.int
is just a "typedef" for whichever is fastest on my system. – Stanleighlong
does not meanlonger than int
(It means that it's at least the size ofint
and possibly longer - Usually not the case). However, C does guarantee that, at a minimum,unsigned int
can hold at least65,535
, whilesigned long
is2,147,483,647
. Though I'd argue you should know more about the architecture of where you're building, this at least guarantees that you'll always be able to hold up to2,147,483,647
number if you uselong
. – Toperchar
is 8-bits,short
is 16-bits,int
is 16-bits, andlong
is 32-bits. So in this caseshort
andint
are identical, whilelong
truly is longer. – Scevour