Previous: 5.1 Representing Numbers
Up: 5.1 Representing Numbers
Next: 5.1.2 Single and Double Precision Floating Point Numbers
Previous Page: 5.1 Representing Numbers
Next Page: 5.1.2 Single and Double Precision Floating Point Numbers
For integer data types, there are three sizes: int, and two additional sizes called long and short, which are declared as long int and short int. The keywords long and short are called sub-type qualifiers. The long is intended to provide a larger size of integer, and short is intended to provide a smaller size of integer. However, not all implementations provide distinct sizes for them. The requirement is that short and int must be at least 16 bits, long must be at least 32 bits, and that short is no longer than int, which is no longer than long. Typically, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits.
Unless otherwise specified, all integer data types are signed data types, i.e. they have values which can be positive or negative. Recall, char types, without qualifiers, may be signed or unsigned depending on the implementation. However, all sizes of integers and char type may be explicitly qualified as signed or unsigned. (Unsigned numbers are always non-negative numbers).
For integers, long, short, and unsigned may be declared with the keyword int or without it. In C, whenever a data type is left out in a declaration, int is assumed by default. Here are some example declarations:
long int light_year; short int n; signed char ch; unsigned char letter; unsigned int age; long distance; short m, n; unsigned memory_address; unsigned long zip_code;
The data type of a constant, written directly into a program, is ascertained from the way it is written. Integer constants are written as a string of digits, optionally preceded by a unary positive or a negative operator. Commas are not allowed. Decimal integer constants should be written without leading zeros, for example:
29 -173 0 1 -525 +7890Alternate number systems may also be used to express integer constants in C programs. Octal numbers are written with a leading zero, and hexadecimal numbers are written with a preceding zero followed by the letter x or X:
Constants Octal/Hexadecimal IntegersA constant to be represented as a long int may be explicitly written using the suffix l or L, as in:0234 octal number 234 0101 octal number 101 0x34 hexadecimal number 34 0X1F hexadecimal number 1F
123L 45678lAny integer constant that is too big to fit into the integer size is interpreted by the compiler as long.
Unsigned integers can be of all sizes, int, long,
and short.
The range of unsigned integers is through
,
where k is the number of bits, so for 16 bits
the maximum unsigned integer is 65535.
Unsigned integer constants are written using the suffix, u or U:
0xFFFFU 123U 0777uThe two suffixes can be combined to write an unsigned long:
12345678UL 0X8FFF FFFFLU