On modern systems, it's very common either for short and int to be the
same size, or for int and long to be the same size.
Roughly speaking:
char has to be at least 8 bits
short has to be at least 16 bits (and at least as wide as char)
int has to be at least 16 bits (and at least as wide as short)
long has to be at least 32 bits (and at least as wide as int)
There's often an advantage in having predefined integer types of sizes
8, 16, and 32 bits. Without defining additional types, there are
several ways to do this (assuming all sizes are powers of 2):
char 8 bits
short 16 bits
int 16 bits
long 32 bits
or
char 8 bits
short 16 bits
int 32 bits
long 32 bits
or
char 8 bits
short 16 bits
int 32 bits
long 64 bits
You're likely to see the latter only on 64-bit systems.
Even on 64-bit systems, int is likely to be only 32 bits, because
making it 64 bits leaves a hole in the type system (either there's no
16-bit type, or there's no 32-bit type); Cray systems, however, have
64-bit ints anyway.
None of this is guaranteed, and there's seldom any great need to
assume that it is. You can write code that doesn't make any
assumptions beyond what's guaranteed by the standard.
C99 introduces typedefs for exact-width types: int16_t, int32_t,
int64_t, and so forth.
(Above, I wrote "Roughly speaking" because what the standard
guarantees isn't these sizes. Instead, it guarantees certain
representable ranges; either -127..+127 or 0..255 for char,
-32767..+32767 for short and int, -2147483647..+21 47483647 for long.
These usually translate to the specified number of bits, but extra
padding bits are permitted. With padding bits, it's even possible to
have sizeof(short) > sizeof(int), but I don't think any real
implementations do this. Also, for a 16-bit type, the range is more
likely to be -32728..+32767 rather than -32767..+32767; the standard's
minimum ranges allow for sign-magnitude and one's-complement
representations , but two's-complement is much more common these days.)
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.