| || |
The C language defines data types in terms of a minimum size, rather than an exact size . As of this writing, this mainly matters for the types
long . A variable of type
int must be at least 16 bits, and is often 32 bits. A variable of type
long must be at least 32 bits, and is sometimes 64 bits.
The range of a 16 bit number is -32768 to 32767 for a signed number, or 0 to 65535 for an unsigned number. If a variable may hold numbers larger than 16 bits, use
long rather than
int . Never assume that
long have a specific size, or that they will overflow at a particular point. When appropriate, use variables of system defined types rather than
- Use this to hold the size of an object, as returned by
- Use this to hold the difference between two pointers into the same array.
- Use this to hold a time value as returned by the
- On a Unix system, use this to hold a file position as returned by
- Use this to hold the result of the Unix
Some books on C recommend using typedefs to specify types of particular sizes, and then adjusting those typedefs on specific systems. GNU Autotools supports this using the `AC_CHECK_SIZEOF' macro. However, while we agree with using typedefs for clarity, we do not recommend using them purely for portability. It is safest to rely only on the minimum size assumptions made by the C language, rather than to assume that a type of a specific size will always be available. Also, most C compilers will define
int to be the most efficient type for the system, so it is normally best to simply use
int when possible.