john said:
I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.
Then why would one use an int instead of a short
if short takes less space?
If your 'int' only holds values up to 32,767, then it is surely the same
type as 'short' on your machine. There is no reason to use one over the
other. However, on most machines, 'int' can hold values over 32,767.
Using an int may be faster than using a short. The type int is designed
to represent the native word size of the computer, where possible.
On many 64-bit computers that is no longer the case. If they made 'int'
64 bits, and 'short' remained 16 bits, then there would be no 32-bit
integer type available. If 'short' moved to 32 bits, then there would be
no 16-bit integer type available. The usual solution is to leave 'char'
as 8 bits, 'short' as 16 bits, and 'int' as 32 bits. Most C
implementations on 64-bit hardware do move 'long' to 64 bits, while I
believe some leave 'long' as 32 bits and continue using 'long long' for
the 64 bit type.
The choice of what type to use when programming in C should usually be
based on what range of values you need to store. If 16 bits are
sufficient, use an 'int', if 32 bits are required, use a 'long', and if
64 bits are required, use a 'long long'.
Simon.