Node:Integer variables, Next:, 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.

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.