Node:Integer variables, Next:Declarations, Previous:Variables and declarations, Up:Variables and declarations
Integer variables
C has five kinds of integer. An integer is a whole number (a number without a fractional part). In C, there are a limited number of integers possible; how many depends on the type of integer. In arithmetic, you can have as large a number as you like, but C integer types always have a largest (and smallest) possible number.
char
: A single byte, usually one ASCII character. (See the section on thechar
type below.)short
: A short integer (16 bits long on most GNU systems). Also calledshort int
. Rarely used.int
: A standard integer (32 bits long on most GNU systems).long
: A long integer (32 bits long on most GNU systems, the same asint
). Also calledlong int
.long long
: A long long integer (64 bits long on most GNU systems). Also calledlong long int
.
64-bit operating systems are now appearing in which long integers are 64 bits long. With GCC, long integers are normally 32 bits long and long long integers are 64 bits long, but it varies with the computer hardware and implementation of GCC, so check your system's documentation.
These integer types differ in the size of the integer they can hold and the amount of storage required for them. The sizes of these variables depend on the hardware and operating system of the computer. On a typical 32-bit GNU system, the sizes of the integer types are as follows.
Type | Bits | Possible Values
|
char | 8 | -127 to 127
|
unsigned char | 8 | 0 to 255
|
| ||
short | 16 | -32,767 to 32,767
|
unsigned short | 16 | 0 to 65,535
|
| ||
int | 32 | -2,147,483,647 to 2,147,483,647
|
unsigned int | 32 | 0 to 4,294,967,295
|
| ||
long | 32 | -2,147,483,647 to 2,147,483,647
|
unsigned long | 32 | 0 to 4,294,967,295
|
| ||
long long | 64 | -9,223,372,036,854,775,807
to 9,223,372,036,854,775,807
|
unsigned long long | 64 | 0 to 18,446,744,073,709,551,615
|
On some computers, the lowest possible value may be 1 less than shown
here; for example, the smallest possible short
may be -32,768
rather than -32,767.
The word unsigned
, when placed in front of integer types, means
that only positive or zero values can be used in that variable (i.e. it
cannot have a minus sign). The advantage is that larger numbers can then
be stored in the same variable. The ANSI standard also allows the word
signed
to be placed before an integer, to indicate the opposite
of unsigned
.